Change axes limits interactively

Hi everybody,
I would like to change the axes limits of my 2D plots in my GUI interactively. This means that it want to click on one of the axes limits (min/max at x or y), then enter a new limit and finally automatically refresh the plot. In other software (e.g. LabVIEW), this functionality is available by default. However, in MATLAB it seems to require a work-around. I already got the hint to try the 'ButtonDownFcn' callback and the 'CurrentPoint' function. However, right now I am struggling to enter the text. If this would work, it should be straight forward as I could simply execute a set('xLimÄ',...) command with the exact same limit I entered as a text before. Therefore, I'd like to ask if anybody knows how to enter text at the location of the 'CurrentPoint'.
Thank you very much for you help!
Best regards, Johannes

2 件のコメント

AstroGuy1984
AstroGuy1984 2017 年 4 月 25 日
I'm confused as to how you wish to query the user for the new limit? Is the idea that the user clicks the axis and gets a prompt to enter the new limit they want? How do you wish that prompt to appear? A popup? In the shell? Once you get that, you can certainly use the set() command you're desiring to use.
As another option, I have a similar need for interactive limits in a GUI I am writing. What I did is to make several fields (four of them) where the user can enter the limits as they desrie and the plot automatically updates. I also have buttons for log/lin scale which are useful for my purposes.
Johannes
Johannes 2017 年 4 月 26 日
Basically, I want to replicate the behavior of other software such as LABVIEW. I attached a series of screenshots showing the axes manipulation for illustration. I already thought about using a popup (many thanks to Joseph for pointing it out as well) but the procedure in Labview is much more convinient to my opinion.

サインインしてコメントする。

 採用された回答

Joseph Cheng
Joseph Cheng 2017 年 4 月 25 日

1 投票

you can try something like this, which really depends on how you want the user to input the different limits
function uiexample
% Create a figure and axes
f = figure('Visible','on');
ax = axes('Units','pixels');
plot(peaks)
xlab = xlabel('x');
ylab = ylabel('y');
set(xlab,'ButtonDownFcn',{@updateplot,ax,1})
set(ylab,'ButtonDownFcn',{@updateplot,ax,2})
infotext = 'click on axis labels to enter new limits';
htext = uicontrol('style','text','string',infotext,...
'position',[0 0 5*numel(infotext) 20])
end
function updateplot(hObject,event,axhandle,XorY)
XLim = axhandle.XLim;
YLim = axhandle.YLim;
name='Input for plot limits';
numlines=1;
switch XorY
case 1
prompt={'Enter Xmin:','Enter Xmax:'};
defaultanswer={num2str(XLim(1)),num2str(XLim(2))};
XLim=cell2mat(inputdlg(prompt,name,numlines,defaultanswer));
axhandle.XLim = str2num(XLim)';
case 2
prompt={'Enter Ymin:','Enter Ymax:'};
defaultanswer={num2str(YLim(1)),num2str(YLim(2))};
YLim=cell2mat(inputdlg(prompt,name,numlines,defaultanswer));
axhandle.YLim = str2num(YLim)';
end
end

3 件のコメント

Joseph Cheng
Joseph Cheng 2017 年 4 月 26 日
so.... why not do this instead. I don't have the time to fine tune it to properly format/catch incorrect entries and make the edit boxes look more like the axes ticks.
function uiexample
% Create a figure and axes
f = figure('Visible','on');
ax = axes('Units','pixels');
plot(peaks)
axpos = get(ax,'Position');
xylim = [ax.XLim; ax.YLim]';
XYlimitpos = [axpos(1)-3 axpos(2)-25 20 20;...
axpos(1)+ax.Position(3)-10 axpos(2)-25 20 20;...
axpos(1)-25 axpos(2)-8 20 20;...
axpos(1)-25 axpos(2)+axpos(4)-8 20 20];
for ind = 1:4
xyedit(ind) = uicontrol('style','edit','Position',XYlimitpos(ind,:),...
'string',xylim(ind),'callback',{@updateplot,ax,ind});
end
end
function updateplot(hObject,event,axhandle,index)
XYLim = [axhandle.XLim;axhandle.YLim]';
XYLim(index)=str2num(hObject.String);
axhandle.XLim = XYLim(:,1)';
axhandle.YLim= XYLim(:,2)';
end
Joseph Cheng
Joseph Cheng 2017 年 4 月 26 日
Johannes
Johannes 2017 年 4 月 27 日
Thank you very much Joseph. This solvent my problem!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeAxes Appearance についてさらに検索

質問済み:

2017 年 4 月 25 日

コメント済み:

2017 年 4 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by