The GUID tutorial is not correct ?
4 次查看(过去 30 天)
显示 更早的评论
Hello,
I followed the tutorial on how to make a GUI, however it doesn't work : when I select "sinc" or "membrane" the plot doesn't change (it's always peaks). The "surf", "mesh" and "contour" work fine.
I believe the problem comes from the switch case. In the tutorial with GUIDE, when I change the switch to (val) instead of (str{val}), and the case to 1, 2 and 3 it works. However, when I followed the programmatic UI tutorial, this trick did not work.
Can you explain to me why it doesn't work, also what is the type of "str" (why do I have to use "{}").
Thank you.
Here is the code :
function simple_gui2
% SIMPLE_GUI2 Select a data set from the pop-up menu, then
% click one of the plot-type push buttons. Clicking the button
% plots the selected data in the axes.
% Create and then hide the UI as it is being constructed.
f = figure('Visible','off','Position',[360,500,450,285]);
% Construct the components.
hsurf = uicontrol('Style','pushbutton',...
'String','Surf','Position',[315,220,70,25],...
'Callback',@surfbutton_Callback);
hmesh = uicontrol('Style','pushbutton',...
'String','Mesh','Position',[315,180,70,25],...
'Callback',@meshbutton_Callback);
hcontour = uicontrol('Style','pushbutton',...
'String','Contour','Position',[315,135,70,25],...
'Callback',@contourbutton_Callback);
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],...
'Callback',@popup_menu_Callback);
ha = axes('Units','pixels','Position',[50,60,200,185]);
align([hsurf,hmesh,hcontour,htext,hpopup],'Center','None');
% Initialize the UI.
% Change units to normalized so components resize automatically.
f.Units = 'normalized';
ha.Units = 'normalized';
hsurf.Units = 'normalized';
hmesh.Units = 'normalized';
hcontour.Units = 'normalized';
htext.Units = 'normalized';
hpopup.Units = 'normalized';
% Generate the data to plot.
Create a Simple UI Programmatically
3-11
peaks_data = peaks(35);
membrane_data = membrane;
[x,y] = meshgrid(-8:.5:8);
r = sqrt(x.^2+y.^2) + eps;
sinc_data = sin(r)./r;
% Create a plot in the axes.
current_data = peaks_data;
surf(current_data);
% Assign the a name to appear in the window title.
f.Name = 'Simple GUI';
% Move the window to the center of the screen.
movegui(f,'center')
% Make the window visible.
f.Visible = 'on';
% Pop-up menu callback. Read the pop-up menu Value property to
% determine which item is currently displayed and make it the
% current data. This callback automatically has access to
% current_data because this function is nested at a lower level.
function popup_menu_Callback(source,eventdata)
% Determine the selected data set.
str = get(source, 'String');
val = get(source,'Value');
% Set current data to the selected data set.
switch str{val};
case 'Peaks' % User selects Peaks.
current_data = peaks_data;
case 'Membrane' % User selects Membrane.
current_data = membrane_data;
case 'Sinc' % User selects Sinc.
current_data = sinc_data;
end
end
% Push button callbacks. Each callback plots current_data in the
% specified plot type.
function surfbutton_Callback(source,eventdata)
% Display surf plot of the currently selected data.
surf(current_data);
3 A Simple Programmatic UI
3-12
end
function meshbutton_Callback(source,eventdata)
% Display mesh plot of the currently selected data.
mesh(current_data);
end
function contourbutton_Callback(source,eventdata)
% Display contour plot of the currently selected data.
contour(current_data);
end
end
1 个评论
Adam
2017-2-1
Speaking of { }, please use the {} Code button when adding code to a question so that it formats correctly.
Ideally you should make use of blank lines also to delimit functions or anything else that makes it easier to read, but the {} Code button isn't magic to do that so I have just done the minimal formatting for you.
Blank lines and white space cost nothing though and make code readable. Make use of them!!
采纳的回答
Adam
2017-2-1
编辑:Adam
2017-2-1
str is a cell array of characters - one cell for each option in the popup. So you use { } to get the characters out of the chosen cell as a string (well, still char type in this case, not the new string data type).
Your code seems to work find for me. The way it is setup you have to click one of the buttons in order for the plot to update after you change the popup though. I don't know if that is the code's intention, but if it isn't then it is just missing a whole instruction from the popup callback.
The code does not look like it is setup at all to handle that situation though as the popup has no way of knowing which of the plot types (surf, mesh, contour) is currently active in order to update it when the popup option changes. That can certainly be done, but it requires more information to be stored from callbacks since the plot types are all push buttons rather than e.g. a mutually exclusive radio box whose current value you could query.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!