MATLAB: Change axes limits interactively

axesbuttondownfcncurrentpointinteractivelylimts

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

Best Answer

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