newbie
newbie

Reputation: 311

handles structure in matlab GUI

I am learning GUI programmin in Matlab.

I found out that it is pretty tricky that the handles structure used for storing all gui Data. I'd like to know how exactly the structure is. I will use an example to explain my question.

let's say, we have created edittext1.

we would have handles.edittext1 stored. then

str = get(hObject,'String');
set(handles.edittext1,'String',str);

this will set String in handles.edittext1 to str.

My question is surprisingly for me, 'String' in the get and set operation is not case sensitive.

And handles.edittext1.String does not return the the stored string.

So, handles is not a structure array, and as a question following with, what structure of handles should it be? And the same question can be asked to hObject as well.

And no, it is not a homework or assignment. It is an ongoing project for my job. I have worked with matlab for years but this is the first time for me to make a GUI with matlab. So, please treat me as a newbie.

regards

Upvotes: 2

Views: 13945

Answers (2)

Amro
Amro

Reputation: 124573

AFAIK all handles to GUI components are objects of classes (OOP) that inherit from the handle superclass (hgsetget to be more exact, which itself is a subclass of handle). Thus hObject stores a pointer to an object, not the object itself.

The handle class and its common subclasses expose common functions (an interface) such as set/get (they perform a case-insensitive partial match for the property name). In this case, you need to access the "property" 'string' using getter/setter methods.

The handles is a variable used in GUIDE-generated GUIs, passed around to callback functions for the convenience of the user. It is a simple structure (not a structure array), thus each field can store different types of data.

Note: MATLAB has an excellent documentation explaining all of this in detail. I will updated the answer later with links to relevant pages..

Upvotes: 0

Jonathan
Jonathan

Reputation: 614

hObject is the handle to the object that issued the callback (e.g. the button which initiated the callback when clicked). handles is a structure containing handles to all the objects in your figure. hObject will be different for each object, the same handles list will be passed to every callback. In your example, if that is in the edittext1 callback, hObject and handles.edittext1 are the same.

Both are handles to objects, not structures. So you can't say handles.edittext1.String. All the properties of the objects (like the string contents) are accessed through the set and get functions. As you observed, the property names are not case-sensitive. Hope that answered all your questions.

Update:

My response to your follow up question was too long for the comment, so I updated the answer.

I'm not sure I understand your question. If you want to check the value of a property, you use get as you showed in your question:

str = get(handles.edittext1, 'String');

If you want to check if a property exists or what the properties are, call get without specifying a parameter:

get(handles.edittext1)

This will print all the properties and their values to the console. If you want to do this programmatically,

params = get(handles.edittext1);

will return a structure whose fields are the parameters. You can then access the parameters as

str = params.String

and can check if a field exists with

 isfield(params, 'String');   // This is case sensitive

Upvotes: 4

Related Questions