필터 지우기
필터 지우기

Change x-axis with uicontrol slider

조회 수: 11 (최근 30일)
Daniel
Daniel 2014년 12월 8일
편집: Adam 2014년 12월 12일
Hello, I want to plot x vs y and be able to use a slider to change the value on the x-axis x goes from 0 to 1000 in my example. I want to use a slider to interactive change the x-axis so I only look at for example 100 to 200 or something like that.
function uicontrol
figure
x=[0:1000];
y=x*100;
hax=plot(x,y)
uicontrol('Style', 'slider',...
'Min',1,'Max',50,'Value',41,...
'Position', [400 20 120 20],...
'Callback', {@xlim,hax});
uicontrol('Style','text',...
'Position',[400 45 120 20],...
'String','change x')
end
function xlim(hObj,x)
xlim(x,[0 20]);
end
I get the following error message
Error using Slider>xlim Too many input arguments.
Error while evaluating uicontrol Callback
I could use some help, I am new to uicontrol and sliders

채택된 답변

Adam
Adam 2014년 12월 12일
편집: Adam 2014년 12월 12일
To answer your last post which should have been a comment rather than an answer:
hax = plot(x,y)
returns a handle to the line object(s) that was plotted, not the axes on which it/they was plotted.
Also your callback syntax still looks wrong. The following should work, with variant depending on what you want:
figure; hAxes = gca;
plot( 1:25, rand(1,25) )
uicontrol('Style', 'slider', 'Min',1,'Max',50,'Value',41, 'Position', [400 20 120 20], 'Callback', @(src,evt) hax( src, hAxes ) );
function hax( src, hAxes )
xlim( [0 get( src, 'Value' )] )
  댓글 수: 1
Daniel
Daniel 2014년 12월 12일
Thank you. I got the slider to work with your comments. Now I can adjust and evolve something on my own.

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

추가 답변 (2개)

matt dash
matt dash 2014년 12월 8일
All callbacks come with TWO builtin inputs. you forgot the 2nd "eventdata" input. You should have:
function xlim(hObj,eventdata,x)
  댓글 수: 2
Daniel
Daniel 2014년 12월 9일
Thank you
So now I have
function xlim(hObj,eventdata,x)
xlim(x,[0 20]); end
But it still give me the same error message. What am I still doing wrong?
matt dash
matt dash 2014년 12월 9일
편집: matt dash 2014년 12월 9일
Oh, well now i see another problem. You named your function xlim, and in it you try to call the builtin function xlim. You can't have both. The error is now happening because the xlim(x,[0 20]) is trying to call your 3-input xlim function, so once again it has too few inputs.
Just name your function anything other than xlim and you'll be fine.
Edit: If you don't want to rename your function, the other option is to call the builtin function to explicitly tell your code which version of xlim you're trying to call:
builtin('xlim',x,[0 20])

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


Daniel
Daniel 2014년 12월 12일
Thank you for your support Matt. I still can not get it to work.
My code now looks like this
%function uicontrol
clear all
delete all
figure
x=[0:1000];
y=x*100;
hax=plot(x,y)
% xlim([0 100]); uicontrol('Style', 'slider',... 'Min',1,'Max',50,'Value',41,... 'Position', [400 20 120 20],... 'Callback', {@xg,hax}); uicontrol('Style','text',... 'Position',[400 45 120 20],... 'String','change x') end
function xg(hObj,eventdata,x)
xg(x,[0 20]);
end

카테고리

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