Understanding guidata and handles

10 visualizaciones (últimos 30 días)
Josh
Josh el 5 de Mayo de 2016
Respondida: Geoff Hayes el 6 de Mayo de 2016
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

Respuestas (1)

Geoff Hayes
Geoff Hayes el 6 de Mayo de 2016
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!

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by