How to understand gui handles ?
47 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
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
2014-9-2
编辑:Adrien
2014-9-2
1 个评论
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 Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!