Understanding guidata and handles

11 次查看(过去 30 天)
Josh
Josh 2016-5-5
I've tried reading the documentation and about 400 tutorials on the use of guidata and handles when programmatically creating a matlab gui, but something just isn't clicking. I'm creating a gui that is initialized with a number of buttons, axes, textboxes etc. and would like to gather these objects (i.e. gui data) in a structure, such as the commonly referred to handles structure. Using [s/g]etappdata works, but becomes tedious with many objects.
Below I have copied a portion of code from the Mathworks website. Is it possible to use guidata, guihandles or a similar command to create a handles structure that stores the hsurf, htext... hpopup objects? So that I could then simply pass 'handles' as input to my callback functions? For example, if inside a callback function I wanted to assess if a different object is 'Checked', or if its 'Value' is 0 or 1, then simply passing 'handles' to the callback will give me access to the other object and its properties.
I've made a number of attempts such as guidata(f), guihandles(gcf) etc. Rather than trying to guess the right command I'd prefer to understand how getting this data works. I realize I can probably just save these objects as handles.hsurf etc. to begin with, but am I incorrect in believing that guidata could do this for me?
Thank you in advance for any help.
function simple_gui2
% Create a UI
f = figure('Position',[360,500,450,285]);
% Construct the components.
hsurf = uicontrol('Style','pushbutton','String','Surf',...
'Position',[315,220,70,25]);
hmesh = uicontrol('Style','pushbutton','String','Mesh',...
'Position',[315,180,70,25]);
hcontour = uicontrol('Style','pushbutton',...
'String','Contour',...
'Position',[315,135,70,25]);
htext = uicontrol('Style','text','String','Select Data',...
'Position',[325,90,60,15]);
hpopup = uicontrol('Style','popupmenu',...
'String',{'Peaks','Membrane','Sinc'},...
'Position',[300,50,100,25]);
ha = axes('Units','Pixels','Position',[50,60,200,185]);
align([hsurf,hmesh,hcontour,htext,hpopup],'Center','None');
handles=guidata(f); % An attempt to collect the objects in a handles structure. This returns an empty array.
handles=guidata(gcf) % Again, handles array is empty
end

回答(1 个)

Geoff Hayes
Geoff Hayes 2016-5-6
Josh - since you are programmatically creating your GUI, you could avoid the use of the handles structure and calls to guidata (which may not be applicable to this GUI design). Instead, nest your callback functions within the main GUI function (in this case, the simple_gui2) so that these functions can access the variables declared within the "parent" function. For example, if we add a callback to your surf button, we could then draw something on the axes as
function simple_gui2
% Create a UI
f = figure('Position',[360,500,450,285]);
% Construct the components.
hsurf = uicontrol('Style','pushbutton','String','Surf',...
'Position',[315,220,70,25], 'Callback',@surfCallback);
hmesh = uicontrol('Style','pushbutton','String','Mesh',...
'Position',[315,180,70,25]);
hcontour = uicontrol('Style','pushbutton',...
'String','Contour',...
'Position',[315,135,70,25]);
htext = uicontrol('Style','text','String','Select Data',...
'Position',[325,90,60,15]);
hpopup = uicontrol('Style','popupmenu',...
'String',{'Peaks','Membrane','Sinc'},...
'Position',[300,50,100,25]);
ha = axes('Units','Pixels','Position',[50,60,200,185]);
align([hsurf,hmesh,hcontour,htext,hpopup],'Center','None');
function surfCallback(hSource, eventdata)
[X,Y,Z] = peaks(25);
surf(ha, X, Y, Z);
end
end
Note how ha is used from within the callback. Try the above and see what happens!

类别

Help CenterFile 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