Handle the handles in guidata

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 :

  1. Not elegant: this global variable is only meaningful to your program and not elsewhere, so why would you make it GLOBAL?
  2. 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 :

get(h)

Try it out.  Just create a figure and see what is stored in the figure :

h=figure;
get(h)

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

handles=guidata(h);

In my example, I get :

handles =
figure1: 350.0012
OpenHelp: 179.0015
SettingsPanel: 177.0015
ValidateValues: 176.0018
Apply: 351.0012
Text: 178.0015
output: 350.0012

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 :

handles=guidata(h);
handles.NewVariable=rand(10);

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?

Related Posts

This entry was posted in Intermediate, Making interfaces. Bookmark the permalink.

14 Responses to Handle the handles in guidata

  1. Sonali says:

    i can understand this now…..thank you very much sir!!!

  2. Natalia Anga says:

    I have one question. One of the variables I am storing in the handles of my GUI is an structure that contains the location ( directory and name) of a folder of images selected by the user. This can be on the order of 130.000 ….My worry is that if I pass the handles to a subfunction that modifies the actual handle structure, matlab will create a copy of handles (because i modified it) and then i may start generating memory problems.
    As i understand matlab only creates a copy of a variable inside a funciton if such variable is modified. Not sure what the case is with the handles structure.

    Would you rather have this structure with the image location as part of the handles or a separate variable.

    All suggestions really appreciated ( at the moment i have it as part of my handles just wandering…).

  3. Per definition, handles is a structure of handle indeed, not a handle (In Matlab handle is like a pointer). So if you add a new field to handles, then it is like a normal field of structure. As you said, making a copy or modifying it will temporally duplicate it in memory.
    In your case, as an easy solution, I would pass this huge variable through global or persistent variables.
    Another more complicated solution would be to define an object oriented object as an handle class for your object.

    Also 130 000 list of paths should be in the 100 MB which is not necessarily crazy in memory.
    I would add that storing all these filenames does not seem very nice. Maybe there is way not to have to store 130 000 paths? Especially if there are all in the same folder?

  4. Neha says:

    Thank YOU so much for the details…this is the first I have understood what handle is in MATLAB. Thank you very much

  5. Dani says:

    Thanks for this article! This was exactly my problem, although your suggestions ended up leading me to using guihandles instead of guidata, since that’s the existing pointer to all the handles in the GUI

  6. Mau says:

    Hi
    Thanks for the post. I’d like to add that if you store very large amounts of data in handles it can cause a slowdown in your gui (had this problem firsthand).

    I suppose a copy of handles is made whenever guidata is called.

    The solution I’m going to try is to use setappdata(0,..) and getappdata(0,..) to save data within my guis.

  7. Amit says:

    can we store data in gui interface and can access it even after closing a gui?

  8. Pedro says:

    awesome tips, you just made my life so much easier, thx

  9. Ayu says:

    1. Value of handles.validate value is 176.0018 ,where does thi come from
    2. I am not able to understand this sub-function problem . please explain it with example.

  10. Ayu says:

    why handles.output is equal to h whre h is handle to complete gui figure

  11. Ayu says:

    what is the difference between object handles and h ?
    please explain my all doubts , i am new to matlab. I would be very thankful.

  12. A A says:

    Thanks a lot! You saved me a lot of time with this explanation!!! :)

  13. arun says:

    This is the best explanation, i ever

Leave a Reply