필터 지우기
필터 지우기

Slider Using GUIDE with Code Example

조회 수: 3 (최근 30일)
Amanda
Amanda 2012년 9월 3일
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

채택된 답변

Image Analyst
Image Analyst 2012년 9월 3일
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
Amanda
Amanda 2012년 9월 3일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by