필터 지우기
필터 지우기

MATLAB : user choose the limits of the xaxis

조회 수: 3 (최근 30일)
Sarah Guiffray
Sarah Guiffray 2015년 3월 31일
댓글: Brendan Hamm 2015년 3월 31일
Hello,
I have a graph in a figure and I would like to put some buttons to allow the user to choose the beginning of xaxis and the end.
For example, if my x axis is 1:1:10. And the user choose xmin=2 and xmax=6. I would like to plot only this part. After, with a button reset, all the courb will be plot again.
How can I do that ?
Thank you in advance,
Best regards,

채택된 답변

Brendan Hamm
Brendan Hamm 2015년 3월 31일
편집: Brendan Hamm 2015년 3월 31일
The following code creates a plot of the input data. It has 2 text boxes which are editable and upon pushing the 'Update' button will change the XLim property of the axes.
function btnUpdate(x,y)
f = figure;
a = axes('Position',[0.075 0.25 0.85 0.65]);
p = plot(x,y);
lwLim = uicontrol(f,'Style','edit',...
'Units','Normalized','Position',[0.20 0.1 0.2 0.05]);
lwTxt = uicontrol(f,'Style','text',...
'Units','Normalized','Position',[0.10 0.09 0.1 0.05],...
'String','xMin:');
upLim = uicontrol(f,'Style','edit',...
'Units','Normalized','Position',[0.60 0.1 0.2 0.05]);
upTxt = uicontrol(f,'Style','text',...
'Units','Normalized','Position',[0.50 0.09 0.1 0.05],...
'String','xMax:');
btn = uicontrol(f,'Style','pushbutton',...
'Units','Normalized','Position',[0.85 0.05 0.1 0.05],...
'String','Update','Callback',@btnCallback);
function btnCallback(src,evt)
a.XLim = [str2num(lwLim.String) str2num(upLim.String)];
end
end
For a sample call:
x = -3*pi:pi/50:3*pi;
y = sin(x);
btnUpdate(x,y);
Play with the axes and have fun!
  댓글 수: 1
Brendan Hamm
Brendan Hamm 2015년 3월 31일
I should point out in versions prior to 2014b the callback function line should read:
set(a,'XLim',[str2num(lwLim.String) str2num(upLim.String)]);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by