uicontrol question: Slider only acts on last Figure, does not act on Figure it's associated with.
Show older comments
MATLAB version: R2012b
I'm trying to learn uicontrol using the example given from http://www.mathworks.com/help/matlab/ref/uicontrol.html
I put the example into a for loop running 3 times and get 3 plots.
Question 1: I get Figure1, Figure2, & Figure5. Why Figure5? Why does it skip 3 & 4?
Question 2: The pop-up menu and the push button both control the plot the buttons are on (which is the action I'm wanting) but the sliders on all 3 plots only controls the last plot made (i.e., Figure5).
Restating - the pop-up menu & push button on Figure1 will control the Figure1 plot, and likewise for Figure 2 & 5, but the slider on Figure1 controls the Figure 5 plot, and likewise the slider on Figure2.
I want the slider to behave like the pop-up and push button and control the Figure they're placed on. I thought perhaps that ax needed to be a matrix referenced to ii, i.e., ax(ii) but that didn't help. Note that when I looped it twice (and only have a Figure1 & Figure2) I have the same issue.
Here's the code:
function myui
for ii = 1:3
% Create a figure and axes
f = figure('Visible','off');
ax = axes('Units','pixels');
surf(peaks)
% Create pop-up menu
popup = uicontrol('Style', 'popup',...
'String', {'parula','jet','hsv','hot','cool','gray'},...
'Position', [20 340 100 50],...
'Callback', @setmap);
% Create push button
btn = uicontrol('Style', 'pushbutton', 'String', 'Clear',...
'Position', [20 20 50 20],...
'Callback', 'cla');
% Create slider
sld = uicontrol('Style', 'slider',...
'Min',1,'Max',50,'Value',41,...
'Position', [400 20 120 20],...
'Callback', @surfzlim);
% Add a text uicontrol to label the slider.
txt = uicontrol('Style','text',...
'Position',[400 45 120 20],...
'String','Vertical Exaggeration');
% Make figure visble after adding all components
set(f,'Visible','on');
end %end for ii
function setmap(source,callbackdata)
% source = handle to the object that was used to call the function
% callbackdata is the event associated with the object.
val = get(source,'Value');
maps = get(source,'String');
newmap = maps{val};
colormap(newmap);
end
function surfzlim(source,callbackdata)
val = 51 - get(source,'Value');
zlim(ax,[-val val]);
end
end %end function myui
Accepted Answer
More Answers (1)
Walter Roberson
on 20 May 2015
0 votes
Prevent confusion. Always "Parent" your graphics operations. See http://uk.mathworks.com/matlabcentral/answers/22208-show-figure
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!