Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Handles

Subject: Handles

From: Chris

Date: 10 Dec, 2008 23:37:02

Message: 1 of 5

Hi all,
I've been trying to learn about the 'handles' thing that appears in GUIDE scripts I've downloaded and in scripts I've downloaded from here. I just can't seem to get my head around it.
I have uploaded an example of my script where I haven't used this at all but it needs to be reviewed - I'll add the link when it becomes available.

However, in the meantime, an example of a function nested within my GUI is:
    m3=uicontrol(fig,...
        'style','pop');
    function calcbutton(source,eventdata); %push to calculate button
        str3=get(m3,'string');
        val3=get(m3,'value');
    end

I assume, using handles provides benifits which aren't apparent at the moment. Any advice on where to use this or where to research it would be greatly appreciated.

An example of the code I'm struggling to understand:
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)
saveState(handles)

I have read the help files on GUIDATA and GUIHANDLES but still don't understand it I'm afraid.

Cheers,
Chris

Subject: Handles

From: Jiro Doke

Date: 11 Dec, 2008 03:05:05

Message: 2 of 5

"Chris " <chris.crawshaw@gmail.com> wrote in message <ghpjqu$7jn$1@fred.mathworks.com>...
> Hi all,
> I've been trying to learn about the 'handles' thing that appears in GUIDE scripts I've downloaded and in scripts I've downloaded from here. I just can't seem to get my head around it.
> I have uploaded an example of my script where I haven't used this at all but it needs to be reviewed - I'll add the link when it becomes available.
>
> However, in the meantime, an example of a function nested within my GUI is:
> m3=uicontrol(fig,...
> 'style','pop');
> function calcbutton(source,eventdata); %push to calculate button
> str3=get(m3,'string');
> val3=get(m3,'value');
> end
>
> I assume, using handles provides benifits which aren't apparent at the moment. Any advice on where to use this or where to research it would be greatly appreciated.
>
> An example of the code I'm struggling to understand:
> 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)
> saveState(handles)
>
> I have read the help files on GUIDATA and GUIHANDLES but still don't understand it I'm afraid.
>
> Cheers,
> Chris

Chris,

Is it okay to assume that you read through the "Creating GUIs with GUIDE" section of the documentation?

http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/bqz6p81.html

The above page is also found by typing the following in the Command Window:

web([docroot,'/techdoc/creating_guis/bqz6p81.html'])

One thing to note is that GUIDE-generated files don't use Nested Functions, so mixing them inside may have some adverse effects. "handles" variable is used to give user the access to various graphics components in your GUI, and it allows you to change and set properties. You can store user data to "handles", and you can use GUIDATA to update "handles" so that other GUI components in your GUI will have access to it.

I suggest you take a look at some of the videos here:

http://blogs.mathworks.com/videos/category/gui-or-guide/

The two videos at the end may give you the basics to help you get your head around the concepts.

jiro

Subject: Handles

From: Steven Lord

Date: 11 Dec, 2008 15:55:20

Message: 3 of 5


"Chris " <chris.crawshaw@gmail.com> wrote in message
news:ghpjqu$7jn$1@fred.mathworks.com...
> Hi all,
> I've been trying to learn about the 'handles' thing that appears in GUIDE
> scripts I've downloaded and in scripts I've downloaded from here. I just
> can't seem to get my head around it.
> I have uploaded an example of my script where I haven't used this at all
> but it needs to be reviewed - I'll add the link when it becomes available.
>
> However, in the meantime, an example of a function nested within my GUI
> is:
> m3=uicontrol(fig,...
> 'style','pop');
> function calcbutton(source,eventdata); %push to calculate button
> str3=get(m3,'string');
> val3=get(m3,'value');
> end
>
> I assume, using handles provides benifits which aren't apparent at the
> moment. Any advice on where to use this or where to research it would be
> greatly appreciated.
>
> An example of the code I'm struggling to understand:
> 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)
> saveState(handles)
>
> I have read the help files on GUIDATA and GUIHANDLES but still don't
> understand it I'm afraid.

Don't worry about GUIDATA and GUIHANDLES for right now. First, have you
read the section in the Getting Started guide that deals with Handle
Graphics objects?

http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f3-15974.html

One metaphor that I find useful is to think of an object's handle as its
telephone number. Using the handle/phone number, you can communicate with
the object using either GET (which allows you to ask the object questions,
like "What is your Position property?") or SET (which allows you to give the
object an order, like "Change your Position property to this vector.").

The way GUIDE defines the callback functions for the objects, it passes in a
handles structure (a telephone book) listing the handles for all the objects
in the GUI. Just like a phone book, a handles structure makes it easier to
find an object's handle/phone number. As the comment says, the handles
structure not only contains handles to objects, but it can also contain user
data (like a note you write inside the phone book.)

If you don't have a copy of the handles structure/phone book in your
function, you can retrieve one with GUIDATA or GUIHANDLES, and if you want
to copy the additional data/notes you've made in your local copy of the
handles structure to the main handles structure that the figure keeps for
its reference, you can also use GUIDATA and GUIHANDLES for that purpose.

It's not a perfect metaphor (for instance, you can change your telephone
number but you can't really change an object's handle) but as metaphors go,
it's not half bad if I do say so myself.

--
Steve Lord
slord@mathworks.com

Subject: Handles

From: Chris

Date: 12 Dec, 2008 01:09:02

Message: 4 of 5

"Steven Lord" <slord@mathworks.com> wrote in message <ghrd59$9vb$1@fred.mathworks.com>...
>
> "Chris " <chris.crawshaw@gmail.com> wrote in message
> news:ghpjqu$7jn$1@fred.mathworks.com...
> > Hi all,
> > I've been trying to learn about the 'handles' thing that appears in GUIDE
> > scripts I've downloaded and in scripts I've downloaded from here. I just
> > can't seem to get my head around it.
> > I have uploaded an example of my script where I haven't used this at all
> > but it needs to be reviewed - I'll add the link when it becomes available.
> >
> > However, in the meantime, an example of a function nested within my GUI
> > is:
> > m3=uicontrol(fig,...
> > 'style','pop');
> > function calcbutton(source,eventdata); %push to calculate button
> > str3=get(m3,'string');
> > val3=get(m3,'value');
> > end
> >
> > I assume, using handles provides benifits which aren't apparent at the
> > moment. Any advice on where to use this or where to research it would be
> > greatly appreciated.
> >
> > An example of the code I'm struggling to understand:
> > 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)
> > saveState(handles)
> >
> > I have read the help files on GUIDATA and GUIHANDLES but still don't
> > understand it I'm afraid.
>
> Don't worry about GUIDATA and GUIHANDLES for right now. First, have you
> read the section in the Getting Started guide that deals with Handle
> Graphics objects?
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f3-15974.html
>
> One metaphor that I find useful is to think of an object's handle as its
> telephone number. Using the handle/phone number, you can communicate with
> the object using either GET (which allows you to ask the object questions,
> like "What is your Position property?") or SET (which allows you to give the
> object an order, like "Change your Position property to this vector.").
>
> The way GUIDE defines the callback functions for the objects, it passes in a
> handles structure (a telephone book) listing the handles for all the objects
> in the GUI. Just like a phone book, a handles structure makes it easier to
> find an object's handle/phone number. As the comment says, the handles
> structure not only contains handles to objects, but it can also contain user
> data (like a note you write inside the phone book.)
>
> If you don't have a copy of the handles structure/phone book in your
> function, you can retrieve one with GUIDATA or GUIHANDLES, and if you want
> to copy the additional data/notes you've made in your local copy of the
> handles structure to the main handles structure that the figure keeps for
> its reference, you can also use GUIDATA and GUIHANDLES for that purpose.
>
> It's not a perfect metaphor (for instance, you can change your telephone
> number but you can't really change an object's handle) but as metaphors go,
> it's not half bad if I do say so myself.
>
> --
> Steve Lord
> slord@mathworks.com
>
Hi,
I think I'm starting to understand this now - your phone number/book analogy really explains it well.
At the moment I don't mind creating GUIs by coding them directly so I don't need to use the handles structure, however I could see it being very useful if you could create more than one (such as a phone book for business contacts and one for personal contacts - perhaps with some contacts in both?) if this is possible it could be useful, for example pushing button_a sets the 3 drop down menu's in handles_a to value 2 without having to type the set function for each menu.
Just a thought but I have a feeling its not possible to group widgets in this way.

Thanks for your advice, and Jiro. The documentation sections are handy, and the video tutorials are always interesting and I always learn something from them.

As a side question, are there other benifits to creating GUIs in GUIDE other than the speed of drag+drop and positioning? And are there any significant draw-backs to using GUIDE?
What are people's general preference?

Cheers,
Chris

Subject: Handles

From: matt dash

Date: 12 Dec, 2008 04:51:02

Message: 5 of 5


> Hi,
> I think I'm starting to understand this now - your phone number/book analogy really explains it well.
> At the moment I don't mind creating GUIs by coding them directly so I don't need to use the handles structure, however I could see it being very useful if you could create more than one (such as a phone book for business contacts and one for personal contacts - perhaps with some contacts in both?) if this is possible it could be useful, for example pushing button_a sets the 3 drop down menu's in handles_a to value 2 without having to type the set function for each menu.
> Just a thought but I have a feeling its not possible to group widgets in this way.
>
> Thanks for your advice, and Jiro. The documentation sections are handy, and the video tutorials are always interesting and I always learn something from them.
>
> As a side question, are there other benifits to creating GUIs in GUIDE other than the speed of drag+drop and positioning? And are there any significant draw-backs to using GUIDE?
> What are people's general preference?
>
> Cheers,
> Chris


GUIDE or no guide, you won't get very far without using handles. (by which i mean the handles structure as it is called in guide, not the plural of "handle") I didnt read the whole posts above cause it's late here, but the handles structure is basically just a way to group all your variables (whether they are actual handle(s) or not) into a single variable so that it's easy to pass it around to all your functions. Since they dont all share a common workspace you can just load the handles structure in all your callbacks and they'll all "know the phone numbers" of all the objects in your gui.

basically if you code your gui without guide you'd need to preface all your uicontrol calls with handles:
handles.pushbutton1=uicontrol('style','pushbutton',...)

in guide it does this for you and stores the handle to the object with tag XXX in handles.XXX.

 you can also store any data you want shared between objects in the handles structure, just do handles.data=... or something. basically the handles structure, along with the uicontrols/figure themselves are the only things that persist throughout a gui's life, so anything you don't store in handles will disappear when its function ends.

After every time you change handles, you need to "save" it with the command guidata(XXX,handles) where XXX is the handle to any uicontrol in your figure (or the handle of the figure itself). this distributes the new phone book to all uicontrols. so make this the last line of any function that adds a new variable to the handles structure. (only need to save it once per function, after all changes have been made)

 But then in every function you still need to load handles as a variable. you could declare it as a third input (all callbacks have two inputs by default), but that gets messy (also you cant just call it "handles", you have to write "guidata(gcbo)" to make sure it loads the new version. if you just write "handles" it'll load whatever version was current when you created the object). Rather than try to change the number of inputs to the callbacks, I find it easier to just make the first line of the callback "handles=guidata(hObject)" where hObject is the first input to the callback.

if you want opinions on guide/coding, just search for guide, it's a pretty hot topic...

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us