MATLAB GUI calculator error - matlab

I'm Developing a calculator that converts back and forth from a Julian time to a standard IRIG time using MATLAB's GUIDE. When started, the calculator works fine going one way, or starting out going the other way, but somehow something gets deleted when going back and forth in the same session. I'm only using two buttons, and this is what the code looks like for the callbacks of those two buttons:
% --- Executes on button press in convertjulian.
function convertjulian_Callback(hObject, eventdata, handles)
% hObject handle to convertjulian (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
value = handles.isec;
day = floor(value/86400);
remainder = (value/86400 - day)*86400;
hour = floor(remainder/3600);
remainder = (remainder/3600 - hour)*3600;
min = floor(remainder/60);
sec = (remainder/60 - min)*60;
set(handles.jday,'String',day);
set(handles.jhour,'String',hour);
set(handles.jmin,'String',min);
set(handles.jsec,'String',sec);
Here is the other callback:
% --- Executes on button press in convertirig.
function convertirig_Callback(hObject, eventdata, handles)
% hObject handle to convertirig (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
seconds=handles.jday*86400+handles.jhour*3600+handles.jmin*60+handles.jsec;
set(handles.isec,'String',sprintf('%0.3f',seconds));
And here is the error that I get in MATLAB when I'm running it:
Error using handle.handle/set
Invalid or deleted object.
Error in timeconversion>convertjulian_Callback (line 124)
set(handles.jday,'String',day);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in timeconversion (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)timeconversion('convertjulian_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback

You are treating handles as numeric values. To get the value from a handles.isec, you can use:
value = str2double(get(handles.isec, 'String'));
All the 'j' handles will be similar.

Related

MATLAB - passing new value of parameter from GUI into .m script

I want to make simple GUI for my script, where I can edit parameter values and running that script.
I've created example scipt and GUI with 2 buttons. I'cant put script code into GUI code, I will need to aply it on much larger script.
So, script code:
number = 10;
variable(1:10) = NaN;
for i = 1:10;
variable(i) = i * number;
end
figure
plot(variable)
Push button code, that is working fine. script is name of .m file, not function:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
evalin('base','script')
But I dont know what to type into edit button code If i want to change value of "number" in the script:
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
And last thing, sometimes when I try to plot more graphs, one figure overwrites GUI figure and I can see only buttons, but not whole GUI.
Thank you fot any help.
The hint given in the last piece of code is enough.
a = str2double(get(hObject,'String'));
This will save the input value as a double, in the callback function's stack.
In order to pass this value to the caller script's (base) stack, use assignin
assignin('base', 'number', a)

Error in loading variables from GUI to workspace in Matlab

I have created a GUI in Matlab and made a simple pushbutton function in GUI for running my matlab file.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Uncertainty_MMS_refactored_Final;
I can load my text files (e.g. PTX_Data_Raw.txt) via GUI in workspace by using assignin function.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
[filename_2,PathName]=uigetfile({'*.txt'},'Import Reference PTX file without header');
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
PTX_Raw = importdata(filename_2);
assignin('base', 'PTX_Data_Raw', PTX_Raw);
content = filename_2;
set(handles.edit2, 'string', content,'fontsize',12);
But when I run my code via GUI byy pusshing "push button" button I receive this error:
Undefined function or variable 'PTX_Data_Raw'
while I check my workspace, I can see all files that I have loaded from GUI to workspace and even I can run my program with F5 button via keyboard! but I don't know why I can run my code via GUI?! HERE is the code that I have written for running my matlab file. here is the full error that I receive:
Undefined function or variable 'PTX_file'.
Error in Uncertainty_MMS_refactored_Final (line 21)
RefOrgnizedData = OrgnizingData(PTX_file, PTX_Data_Raw);
Undefined function or variable 'PTX_file'.
Error in GUI>pushbutton7_Callback (line 178)
Uncertainty_MMS_refactored_Final;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)GUI('pushbutton7_Callback',hObject,eventdata,guidata(hObject))
Error using waitfor
Error while evaluating UIControl Callback
Should I change or add something else more? please help me

Creating a new field in a MatLab guide handles

I am starting with MatLab.
I created a guide UI. I went ahead and assumed I could just add new feild to the handles structure created.
Can I do this?
For example, when I refer to handle.s in my program, I get an error that I am reffering to a non existing feild.
handles.s was created to store a serial port object generated in a function that initialises serial communication between my PC and a microcontroller.
the serial port object has methods and feild of its own...could it be that I cannot pass an object as a feild to the handles object that contains the property of the guide UI?
Here is the code I am working with
function [ s, flag] = setupSerial(comPort)
%Initialize serial port communication between Arduino and Matlab
%Ensure that the arduino is also communicating with Matlab at this time.
%if setup is complete then the value of setup is returned as 1 else 0.
flag =1;
s = serial(comPort);
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
fopen(s);
a='b';
while (a~='a')
a=fread(s,1,'uchar');
end
if (a=='a')
disp('serial read');
end
fprintf(s,'%c','a');
mbox = msgbox('Serial Communication setup.'); uiwait(mbox);
fscanf(s,'%u');
end
Within my UserInterface.m file, I pass over the object in a claaback function as follows
function SerialBtn_Callback(hObject, eventdata, handles)
% hObject handle to SerialBtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
comPort = get(handles.COMportTxt,'String');
if(~exist('serialFlag','var'))
[handles.s, handles.serialFlag] = setupSerial(comPort);
end
end
I get the error when I press the 'Home' button. Here is the callback funciton
function HomeButton_Callback(hObject, eventdata, handles)
% hObject handle to HomeButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp('home button pressed');
fprintf(handles.s,'%s', 'G2');
set(handles.CurrentPositionTxt, 'String', '0');
end
There error I get is the following
Reference to non-existent field 's'.
Here the GUI for you information
The callback where handles.s is defined is not saving the handles object. You must save it using guidata in order to have it available later in another callback.
function SerialBtn_Callback(hObject, eventdata, handles)
% hObject handle to SerialBtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
comPort = get(handles.COMportTxt,'String');
if(~exist('serialFlag','var'))
[handles.s, handles.serialFlag] = setupSerial(comPort);
end
guidata(hObject,handles)
Hope this helps.

matlab function handle inside gui

I created a GUI layout using Matlabs GUID application, essentially my problem is simple, i have a text box where I want to enter data, and a push button which i want to display that value entered inside the text box, the text box is define as
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myString = get(hObject, 'String')
set(hObject,'Value',str2num(myString));
at this point, i entered some numerical value into the text box, lets say 44, now 44 is stored inside the "Value" element of hObject for this function.
Now I want to output this value when a button is pushed
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fHandle = #edit1_Callback
get(fHandle,'Value')
^^^on this last line i should see some output(since i dont have a ";" which allows matlab to print out the data), the issue is....i get an error that says "Conversion to double from function_handle is not possible." , shouldnt I be able to call "get" using a function handle which points to my earlier function, thanks for any help!
I would go for
get(handles.edit1,'value')
All your uicontrols are stored in the handles. Knowing the tag of your uicontrol (for your edit box I guess this is edit1), you can get the handles of this object with handles.edit1.
You can even write
my_value = get(handles.edit1,'Value');

Getting snapshot from webcam in Matlab

I have created a simple GUI to preview webcam stream and to get snapshot from it. For this I have created on axes to show video, one push button(pushbutton1) to start preview, one push button(pushbutton2) to get snapshot. Following is the code for these two push buttons.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
vidObj = videoinput('winvideo',1);
videoRes = get(vidObj, 'VideoResolution');
numberOfBands = get(vidObj, 'NumberOfBands');
handleToImage = image( zeros([videoRes(2), videoRes(1), numberOfBands], 'uint8') );
preview(vidObj, handleToImage);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=getsnapshot(get(axes,'Children'));
imshow(a);
In pushbutton2_Callback I am trying to get child of axes ie. vidObj. But this gives me error ??? Undefined function or method 'getsnapshot' for input arguments of type 'double'.. Why is it returing double type instead of child object vidObj?
How can I fix it and get snapshot?
Is there any other better way?
(I just started learning GUI.)
Thanks.
A better alternative to declaring your variables global, is to use the handles structure for sharing data. GUIDE already uses this structure to store handles to all GUI components. Simply add your data as a field to this structure that gets passed around to all callback functions.
So inside the first callback:
function pushbutton1_Callback(hObject, eventdata, handles)
%# ... your existing code ...
%# store video object in handles, and persist
handles.vidObj = vidObj;
guidata(hObject,handles)
end
Then in the second, you can retrieve the video object from the handles structure:
function pushbutton2_Callback(hObject, eventdata, handles)
frame = getsnapshot(handles.vidObj);
imshow(frame);
end