There are three inputs to every automatically generated callback from GUIDE:
function edit1_Callback(hObject, eventdata, handles)
A common question that I am asked is “What is the handles structure?” This four minute video will show a simple GUI where you edit some text, and press a button that will copy that text into a third uicontrol. This is all done using the handles structure. The other two inputs: hObject and eventdata will be briefly discussed also.
By
Doug Hull
Doug first used MATLAB in 1994, could not figure it out until he got some help in 1995. He is now dedicated to making sure that no one else wastes a year of their life not knowing MATLAB like he did.
We actually had “MATLAB Evangelist” as a title for while when Scott was looking for people. Looks like we took that out of the title, but there are plenty of jobs like that for MATLAB geeks.
About GUIDATA: That is a a different way of interacting with handles. I prefer handles to be more of a read-only type of structure. If I want to store data, then I advocate SETAPPDATA. See my video on this:
There’s actually 6 of us at The MathWorks with an internal title of “LTC Evangelist” (LTC = Language of Technical Computing = MATLAB). I am one of them, and Doug used to be in this group as well.
Doug,
I have a GUI that I put a timer in so that it will check if any serial data has come in every second. I am having trouble getting the Timer to pass the handles structure to my function that is plopped into the M-file that guide made. On compile it says that the “handles” is undefined. I tried to pass the hadles from the Timer funtion by saying {@myfunction, handles} to pass the handles to the myfunction but I get another compile problem. any help would be great.
Doug,
It seems like the handles structure is already there and ready to use. The timer that I made in the OpeningFcn needs to pass the args similar to the widgets on the GUI send, in particular the “handles” part of MyFunct(hObject, eventdata, handles). In the timer object you can specify the args to pass but I think Im having a problem getting the handles to pass through properly. This is what I am trying handles.t = timer(‘TimerFcn’,{@GetSmp_Callback, handles} , ‘Period’, 1,’ExecutionMode’,…
‘fixedspacing’,'startdelay’,1); I initialize this in openingFcn but I am not sure if im handing “handles” properly. Is there a way to use a timer in the GUI and also use the handles structure? I can use set/getappdata but then all the wigets and callbacks use handles structure.
The timer will be running in its own little workspace, so the handles structure that you pass at the beginning of the timer will be whatever it was when you create the timer.
Try this in the opening function:
t = timer(‘TimerFcn’,{@foofoo, handles} , ‘Period’, 1,’ExecutionMode’, …
‘fixedspacing’,'startdelay’,1);
start(t)
With this function on the path.
function foofoo(obj, event, hand)
disp(hand)
A silly little function, but it gets the handles structure out there as you see.
The axes will not be in my main GUI but another new window.
I guess that the reason for this is because, my function is not calling in GUIDE callback scope(it’s called from timer or serial callback). Like it work only if I invoke updateAxes from another callback function from GUI.
How can I do it, the update have to invoke when the new data is come.
Hi Doug, I know this is an old posting, but I am hoping that you are still checking it. I have a question closely related to your video. Let’s say that I create a GUI with GUIDE. In the creation function
SamplingRate_Callback sets a few variables,
handles.a = 1;
handles.b = 2;
handles.c = 3;
The problem I am having is that unless I change the function to
handles = SamplingRate_Callback(handles.SamplingRate, eventdata, handles),
I cannot return the data to the calling function, MyGUI_OpeningFcn. I have tried using guidata(gcf, handles) and guidata(hObject, handles), but neither works.
Would you be able to shed some light on this problem?
Also, I am not sure when to use guidata(gcf, handles) vs. guidata(hObject, handles).
If your calling function, MyGUI_OpeningFcn, wants handles (the new modified one) to be in scope, you will need to do exactly what you are doing by passing it back as an output
*or*
In the called function use GUIDATA to set the handles structure (i.e. guidata(gcf, handles) and then in the calling function refresh the handles with the get form (handles = guidata(gcf).
This is really a scoping of variables issue. Each callback in GUIDE is really just a subfunction and all the scoping of variables rules apply.
I really appreciate your video. It explains me a lot about GUIs that I didn’t understand.
I would like to create a GUI. I have some Matlab scripts with input data. I would like that the user puts this input data in a edit text box and when he pushs the button the script runs. The script loads some files .txt and gives letters to vectors. When the user pushs the button, I would like that the files which are loaded will be saved in matlab workspace.
Can you help me ?
Thanks a lot
Glennie
Leave a Reply
About
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
Nice video as usual. You need a nickname of some sorts . . . I was thinking “The MATLAB Evangelist” might a good one.
But seriously, good videos. I want to start linking them from blinkdagger because I think the tutorials supplement each other very well.
Doug,
Can you explain about guidata(hObject,handles)? I thought this was necessary after any change to handles.
Thanks
Quan,
We actually had “MATLAB Evangelist” as a title for while when Scott was looking for people. Looks like we took that out of the title, but there are plenty of jobs like that for MATLAB geeks.
http://www.mathworks.com/company/jobs/opportunities/ApplicationEngineering.html
Send me a resume, we will see what we can do.
Doug
Naor,
About GUIDATA: That is a a different way of interacting with handles. I prefer handles to be more of a read-only type of structure. If I want to store data, then I advocate SETAPPDATA. See my video on this:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8616&objectType=FILE
Doug
Quan,
There’s actually 6 of us at The MathWorks with an internal title of “LTC Evangelist” (LTC = Language of Technical Computing = MATLAB). I am one of them, and Doug used to be in this group as well.
Hello, Can we output data entered by user in dialogbox to command window?
thanks in advance.
Amir,
You would simply modify the callback to use the DISP command to display any data you want.
Doug
thanks for the idea. it is working.
ıt is very good video. I learn a lot of new things about gui
Doug,
I have a GUI that I put a timer in so that it will check if any serial data has come in every second. I am having trouble getting the Timer to pass the handles structure to my function that is plopped into the M-file that guide made. On compile it says that the “handles” is undefined. I tried to pass the hadles from the Timer funtion by saying {@myfunction, handles} to pass the handles to the myfunction but I get another compile problem. any help would be great.
Zach,
I think that the GETAPPDATA, SETAPPDATA techniques shown here:
http://blogs.mathworks.com/pick/2005/10/03/guide-video-part-two/
Might be an easy solution for you.
Doug
Doug,
I cant get the file to play from link or by going to list??
I have tested, it is working now.
Doug
Doug,
It seems like the handles structure is already there and ready to use. The timer that I made in the OpeningFcn needs to pass the args similar to the widgets on the GUI send, in particular the “handles” part of MyFunct(hObject, eventdata, handles). In the timer object you can specify the args to pass but I think Im having a problem getting the handles to pass through properly. This is what I am trying handles.t = timer(‘TimerFcn’,{@GetSmp_Callback, handles} , ‘Period’, 1,’ExecutionMode’,…
‘fixedspacing’,'startdelay’,1); I initialize this in openingFcn but I am not sure if im handing “handles” properly. Is there a way to use a timer in the GUI and also use the handles structure? I can use set/getappdata but then all the wigets and callbacks use handles structure.
The timer will be running in its own little workspace, so the handles structure that you pass at the beginning of the timer will be whatever it was when you create the timer.
Try this in the opening function:
t = timer(‘TimerFcn’,{@foofoo, handles} , ‘Period’, 1,’ExecutionMode’, …
‘fixedspacing’,'startdelay’,1);
start(t)
With this function on the path.
function foofoo(obj, event, hand)
disp(hand)
A silly little function, but it gets the handles structure out there as you see.
-Doug
Doug,
I want to do the thing like Zach.
I have a GUI that contain many axes.
But when I call the function with timer or serial callback.
when I use like in this example
http://blogs.mathworks.com/pick/2005/10/03/guide-video-part-two/
like this
part from my updateAxes function.
hAxes = findobj(hMainGui, ‘type’, ‘axes’); axes(hAxes(1));
plot ….
The axes will not be in my main GUI but another new window.
I guess that the reason for this is because, my function is not calling in GUIDE callback scope(it’s called from timer or serial callback). Like it work only if I invoke updateAxes from another callback function from GUI.
How can I do it, the update have to invoke when the new data is come.
Thank you very much.
Doug,
Really helpful stuff. Thank-you for taking the time.
Hi Doug, I know this is an old posting, but I am hoping that you are still checking it. I have a question closely related to your video. Let’s say that I create a GUI with GUIDE. In the creation function
MyGUI_OpeningFcn(hObject, eventdata, handles, varargin)
I call another function,
SamplingRate_Callback(handles.SamplingRate, eventdata, handles)
SamplingRate_Callback sets a few variables,
handles.a = 1;
handles.b = 2;
handles.c = 3;
The problem I am having is that unless I change the function to
handles = SamplingRate_Callback(handles.SamplingRate, eventdata, handles),
I cannot return the data to the calling function, MyGUI_OpeningFcn. I have tried using guidata(gcf, handles) and guidata(hObject, handles), but neither works.
Would you be able to shed some light on this problem?
Also, I am not sure when to use guidata(gcf, handles) vs. guidata(hObject, handles).
Thanks for your help!
@BK,
If your calling function, MyGUI_OpeningFcn, wants handles (the new modified one) to be in scope, you will need to do exactly what you are doing by passing it back as an output
*or*
In the called function use GUIDATA to set the handles structure (i.e. guidata(gcf, handles) and then in the calling function refresh the handles with the get form (handles = guidata(gcf).
This is really a scoping of variables issue. Each callback in GUIDE is really just a subfunction and all the scoping of variables rules apply.
Doug
Hi,
I really appreciate your video. It explains me a lot about GUIs that I didn’t understand.
I would like to create a GUI. I have some Matlab scripts with input data. I would like that the user puts this input data in a edit text box and when he pushs the button the script runs. The script loads some files .txt and gives letters to vectors. When the user pushs the button, I would like that the files which are loaded will be saved in matlab workspace.
Can you help me ?
Thanks a lot
Glennie