How to understand gui handles ?

Hello,
I'm programming a GUI. I need help by understanding how handles are working. This is simple code :
Starting creating a Structure S. Storing my figure into it.
S.f = figure('Position', [400,400,1000,400], ...
'Name', 'Store data');
Storing an edit box and a pushbutton.
S.eT_name = uicontrol( 'Style', 'edit', ...
'Position', [400,100,200,50], ...
'FontSize', 15, ...
'BackgroundColor', 'white');
S.pB_save = uicontrol( 'Style', 'pushbutton', ...
'String', 'Save', ...
'Position', [700,75,200,50], ...
'FontSize', 20, ...
'BackgroundColor', 'red');
Setting callbacks.
set(S.pB_save, 'Callback', {@pB_save_callback, S});
set(S.eT_name, 'Callback', {@eT_name_callback, S});
Here are the 2 Callback functions.
function [] = eT_name_callback(hObject, eventdata, S)
s_Name = get(hObject, 'String');
set(S.data, 's_Name', s_Name);
guidata(S.f, S.data);
function [] = pB_save_callback(hObject, eventdata, S)
display(S.data);
My questions are :
- How can i save the string, that I'm getting by the edit box, into my GUI so that I can display it by clicking on the pushbutton ?
- I supposed S is the guihandle, isn't it ? Is there a solution so that i can create for example an array into the GUI to save data ?
Thank you in advance.

 采纳的回答

Adrien - you will want to use the guidata function to set and retrieve user-defined data to the GUI. If you were developing your GUI with GUIDE, this would typically be a structure called handles which is usually an input parameter to all callbacks. Here, you are doing almost the same thing but with a structure called S. (You may want to change your S to handles just to make it clear what this structure contains.)
You have assigned your callbacks as
set(S.pB_save, 'Callback', {@pB_save_callback, S});
set(S.eT_name, 'Callback', {@eT_name_callback, S});
which is fine, but you have to realize that the S that gets passed as the third input parameter to these callbacks is a copy of the S at the time that these callbacks were assigned. So if one callback updates some data in this structure, another callback won't see these changes in S because it receives an out-of-date copy of S. So you can remove the S from each of the above to just have
set(S.pB_save, 'Callback', {@pB_save_callback});
set(S.eT_name, 'Callback', {@eT_name_callback});
We will use guidata to get the most up-to-date handle and user-defined data.
Note that you should add the following line once all handles have been set to S
guidata(S.f,S);
This will save all handles to the GUI so that you can access them at will whenever you call guidata(hObject) from within any of your callbacks.
Your eT_name callback then becomes
function [] = eT_name_callback(hObject, event data)
% get the handles and user-defined data
handles = guidata(hObject);
% set the name field within the (local) handles structure
handles.s_Name = get(hObject, 'String');
% save the data to the (global) handles structure
guidata(hObject, handles);
Note that I have just replaced S with handles and am using hObject to retrieve and set the handles structure.
Your pB_save callback then becomes
function [] = pB_save_callback(hObject, eventdata)
% get the handles and user-defined data
handles = guidata(hObject);
if isfield(handles,'s_Name')
% display the name
display(handles.s_Name);
end
Note how we check to make sure that s_Name is a field within the handles structure before we try to access it.
Of course, the alternative to the accessing handles for s_Name is to just access the widget directly using its handle
display(get(handles.eT_name,'String'));
--------------
The above describes two ways on how you can access the string data from your callback.
S is the structure of GUI widget handles and user-defined data, where the latter is set using the guidata function.
To save an array to the GUI, just do the same thing as you did with the string
handles.arrayData = [1 2 3 4 5];
guidata(hObject,handles);
The above saves an array with five elements, that you can then access in any other callback.
Try the above and see what happens!

3 个评论

Thank you Geoff, this helped me a lot !!
But do I have to place also
guidata(hObject,handles);
when I'm defining my array ? I mean I'm writting :
handles.arrayData = [1 2 3 4 5];
in the function GUI. At the same place where I'm defining the pushbutton or ... Is it not saving it into the handles Automatically ?
One more comprehension question.
guidata(hObject,handles);
By writting this. hObject just means that matlab is saving my data in the figure in wich the Object from the handle hobject is ?
Yes, Adrian - at some point, before "exiting" your function (or GUI body) or wherever you have set
handles.arrayData = [1 2 3 4 5];
you must follow this with a
guidata(hObject,handles);
Just think of handles as a copy of the structure with all widget handles and user-defined data. You must "save" it to the "master" copy using guidata.
From the documentation, guidata(object_handle,data) stores the variable data as GUI data. If object_handle is not a figure handle, then the object's parent figure is used. data can be any MATLAB® variable, but is typically a structure, which enables you to add new fields as required. So yes, MATLAB is saving the data to the parent figure of hObject.

请先登录,再进行评论。

更多回答(1 个)

Adrien
Adrien 2014-9-2
编辑:Adrien 2014-9-2
Geoff, i have an other Problem ... I explain it to you : I have a function called GUI_1 :
function [] = GUI_1()
S.f = figure(...);
S.pB_Start = uicontrol('Style', 'pushbutton', ...);
set(S.pB_Start, 'Callback', {@pB_Start_callback});
function [] = pB_Start_callback(varargin)
S = guidata(varargin{1});
GUI_2(S); % Or GUI_2(guidata(varargin{1})), no difference, right ?
So this was my first GUI/function, which contains only a figure and a pushbutton. Here comes the second :
function [] = GUI(S)
S.pB_1 = uicontrol('Style', 'pushbutton', ...);
S.pB_2 = uicontrol('Style', 'pushbutton', ...);
guidata(S.f, S);
But it is not working. I would like to save the pushbuttons into the guidata of the figure f, so that I can acces it into the GUI_1(). I would like that my S structure also in GUI_1() contains the pushbuttons handles, certainly after having executed GUI_2(S). Is that possible, or do I have to set S as an output of the function GUI_2(S) ?

1 个评论

Adrien - please create a new question for this problem as it does not relate to the first one. As well, describe in more detail what you are trying to accomplish.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by