Slider Using GUIDE with Code Example

5 views (last 30 days)
I want to scroll through multiple plots using a slider
inside GUIDE. I have used this code template that was recommended
to me on the MATLAB's site.
Although this is for external GUI development
and not specifically for GUIDE.
How can I modify it so it will work inside GUIDE?
Here is the code:
function [] = slider_plot()
% Plot different plots according to slider location.
S.fh = figure('units','pixels',...
'position',[300 300 300 300],...
'menubar','none',...
'name','slider_plot',...
'numbertitle','off',...
'resize','off');
S.x = 0:.01:1; % For plotting.
S.ax = axes('unit','pix',...
'position',[20 80 260 210]);
plot(S.x,S.x,'r');
S.sl = uicontrol('style','slide',...
'unit','pix',...
'position',[20 10 260 30],...
'min',1,'max',3,'val',1,...
'sliderstep',[1/2 1/2],...
'callback',{@sl_call,S});
function [] = sl_call(varargin)
% Callback for the slider.
[h,S] = varargin{[1,3]}; % calling handle and data structure.
cla
switch round(get(h,'value'))
case 1
cla
plot(S.ax,S.x,S.x.^1,'r')
case 2
cla
plot(S.ax,S.x,S.x.^2,'b')
case 3
cla
plot(S.ax,S.x,S.x.^3,'k')
otherwise
disp('cannot plot')
end
Thanks,
Amanda

Accepted Answer

Image Analyst
Image Analyst on 3 Sep 2012
Right click your slider in GUIDE and view the callback function. Then put this code in there:
% First get S. See the FAQ for methods to do that.
% http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
sliderValue = int32(get(handles.slider1, 'Value'));
switch sliderValue
case 1
cla;
plot(S.ax,S.x,S.x.^1,'r');
case 2
cla;
plot(S.ax,S.x,S.x.^2,'b');
case 3
cla;
plot(S.ax,S.x,S.x.^3,'k');
otherwise
message = sprintf('No case for slider value of %d', sliderValue);
uiwait(warndlg(message));
end
  1 Comment
Amanda
Amanda on 3 Sep 2012
Image Analyst I really appreciate it.
I was struggling with this last night.
And this morning I was hoping
someone might could help me out. Thanks a lot.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!