Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Handles
Date: Thu, 11 Dec 2008 10:55:20 -0500
Organization: The MathWorks, Inc.
Lines: 67
Message-ID: <ghrd59$9vb$1@fred.mathworks.com>
References: <ghpjqu$7jn$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1229010920 10219 144.212.105.187 (11 Dec 2008 15:55:21 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 11 Dec 2008 15:55:20 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
X-RFC2646: Format=Flowed; Original
Xref: news.mathworks.com comp.soft-sys.matlab:506360



"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