How to understand gui handles ?

37 ビュー (過去 30 日間)
Adrien
Adrien 2014 年 9 月 1 日
コメント済み: Geoff Hayes 2014 年 9 月 2 日
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.

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 9 月 1 日
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 件のコメント
Adrien
Adrien 2014 年 9 月 2 日
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 ?
Geoff Hayes
Geoff Hayes 2014 年 9 月 2 日
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 件のコメント
Geoff Hayes
Geoff Hayes 2014 年 9 月 2 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by