Fasten your seatbelts. Compared to my previous posts which were relatively easy, today I am going to talk about something that confuses 90% of the people I know : GUIDATA.
Guidata is a function used to stored data in the GUI. Yes, you can store data IN your interface. Already this is confusing. I know. This means that if your user actually closes the interface, the data is LOST.
Why is this useful?
Because in some cases, as you design your interface, you might want to share some data between the various functions of your program or between your callbacks. You could use global variables to do this, but in many cases, this is :
- Not elegant: this global variable is only meaningful to your program and not elsewhere, so why would you make it GLOBAL?
- A waste of memory: the global variable is still there after your program is closed.
How does it work?
As I told you before, a handle is like a pointer in Matlab. It usually points at a graphical object that is available on the screen. And if you know a handle h, you can usually get all the underlying data with the command :
Try it out. Just create a figure and see what is stored in the figure :
For instance, get(h,’Position’) will give you the position on the screen.
I already explained all of this in great detail, so why I am telling you this again?
Because a GUI is made of a lot of graphical objects and each one of these objects has a handle. Luckily for you, Matlab provides a way to access all the handles in an interface created with the GUIDE. And this is the job of guidata.
For instance, I created the following GUI using the GUIDE. As you can see, it has 3 buttons and a field in which to put text strings. To access each button, either you know the handle to each one or you use guidata.
The handle to the GUI itself is h, so to access all the handles, just type in
In my example, I get :
As you can see, handles.ValidateValues is the handle to the button (because I named the tag value of this button with this meaningful name).
as a result, get(handles.ValidateValues) will give you all the properties of this particular button.
Please also note that handles.output is equal to h, the handle to the main interface, as I explained before. This is handy, as all the callbacks of your GUI have access to the object handles but they don’t always have access to h directly.
So far so good. Why is this confusing?
Because the way the GUIDE uses this, handles is NOT a handle. Keep this in your head, it is VERY important, It is a STRUCTURE of handles. So you can modify it as you like and add any variable in it. For instance, you could do :
But because it is NOT a handle itself, the structure on the interface is not going to be updated UNTIL you actually update it yourself. So what you need to do is :
handles=guidata(h); handles.NewVariable=rand(10); guidata(h,handles);
This is very confusing and sometimes inconvenient. Because let’s suppose you have a callback that gets the handles with guidata and modifies them. And within this callback, you have a subfunction that also gets the handles. If you have not updated the handles with guidata at the end of your subfunction, your main callback will work with an OLD version of the handles, leading to annoying BUGS.
So this is what you need to do :
function ValidateValues_Callback(hObject, eventdata, handles) % hObject handle to ValidateValues (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Start the subfunction Subfunction(handles); % Now the local handles is not updated handles=guidata(handles.output); % Now after updating, the new Variable is available handles.NewVariable function Subfunction(handles) % modify the local copy of handles handles.NewVariable=rand(10); % Update the handles on the interface guidata(handles.output,handles);
Not really intuitive, is it?