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 :


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 :

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 :


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 :


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

% Now the local handles is not updated

% Now after updating, the new Variable is available

function Subfunction(handles)
% modify the local copy of handles

% Update the handles on the interface

Not really intuitive, is it?

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

30 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:

    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

  14. surekha says:

    Hi, I have created a main figure which only contains menu to open another figure through openfig. In The second figure i have tried acquiring a file name which has to be set in the ‘String’ property of static text. When the second fig is directly opened by calling its name it works fine but when called through the menu of main figure i am unable to set the ‘String’ property of static text. please help me out

  15. pradeep says:

    thanks sir for these nice tutorials. i am new to matlab gui programming. i want to ask that how can i run my gui on a pc with no matlab installed?

  16. pradeep says:

    thanks sir, i am making serial port interfacing gui, how can i make user to enter com port number in edit text box?

  17. Ahmed says:

    This code from matlab help for multiple gui’s
    % — Executes on button press in buttonChangeMe.
    function buttonChangeMe_Callback(hObject, eventdata, handles)
    % hObject handle to buttonChangeMe (see GCBO)
    % eventdata reserved – to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)
    % Call the dialog to change button name giving this figure’s handle
    changeme_dialog(‘changeme_main’, handles.figure);
    my quastion is what does it mean the last line

  18. Mohit says:

    HI Jerome

    Thanks for a great article.

    I hope you could help me in one of a related issue. I have a UI where there is one axes, and one button. At the moment the button click event creates two figures outside the UI. I want to provide a cleaner way for this. I want the figures to be shown in the UI axes. Since there are two plots, I want to give a pop-up menu on UI so that user can select the desired plot to be displayed.

    I was trying to save handles of the two plots. But the moment I get handle object of second plot, the first handle object becomes unidentified. The handle I get is from:

    h1 = plot(x,y);
    set(h1, ‘DisplayName’, ‘Figure 1’);
    new_h1 = copyobj(h1,handles.axes1);

    h2 = plot(y, x);
    set(h2, ‘DisplayName’, ‘Figure 2’);
    new_h2 = copyobj(h2,handles.axes1);

    get(new_h1); // This gives error “Invalid handle object.”

    Any idea on this?

  19. Med aymane ahajjam says:

    Thank you sir, this is very clear!

  20. Rémy NTSHAYKOLO says:

    thank you so much! I struggled so much to understand this “handle” notion!

  21. Alex says:

    Hi Jerome,
    what is the difference between writing
    guidata(hObject,handles),guidata(handles.figure1,handles) and guidata(handles.output,handles) to update the handles?

  22. Manuel J says:

    Hello, could anyone help me?
    Code below captures a graphic inside the main GUI and save it as JPEG file.
    My issue is: at the time I plot the graphic, I store its handle as handle.fig1 and I can use delete(handle.fig1) to erase it anytime.
    For any reason, when I save the graphic I lose the handle (handle.fig1) so I can´t delete it from my GUI.
    How can I keep being able to erase the GUI graphic after saving as picture?

    Thank you beforehand. Regards

    haxes = gca;
    grid on
    [a b] = uiputfile(‘*.jpg’,’Save as JPEG’);
    faux=figure(‘visible’,’off’); % Crea una figura auxiliar no visible
    copyobj(haxes,faux); % Copia solo el contenido del axes
    if a~=0, print(gcf,’-djpeg100′,[b a]);end
    guidata(hObject, handles);

  23. Manuel J says:

    Fixed. Just simplified the previous code.

    New Code:
    [a b] = uiputfile(‘*.jpg’,’Save as JPEG’);
    faux=figure(‘visible’,’off’); % Crea una figura auxiliar no visible
    copyobj(handles.grafica,faux); % Copia solo el contenido del axes
    if a~=0, print(gcf,’-djpeg100′,[b a]);end
    guidata(hObject, handles);

Leave a Reply to Ayu Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.