UIAxes - zoom previous functionality.

Hi All,
I've created an app with integrated UIAxes plot. That is working as expected.
I have added 3 zoom buttons with callbacks to update XLim and YLim.
  1. 'Zoom Auto': resets the zoom to the extents of the data.
  2. 'Zoom Old': resets the axis limits to the previous manual zoomed/panned location.
  3. 'Zoom Tip': zooms to a predefined location of the plot.
The way I have written it, when zoom auto, or tip buttons are pressed, the current XLim and YLim values are saved to XlimOld and YLimOld to be recalled later.
Auto and Tip zooms are working perfectly. the new X and Y limits are set and the ticks recalculate automatically.
When pressing Zoom Old, sometimes it works perfectly, other times, the ticks and positions are not recalculated.
So I thought the problem might occur when dynamically saving the current manual zoom position, but when I debug this seems to work correctly. So maybe this is a timing issue??
I have also tried moving the saveXYLim(app) code directly into the relevent callback functions, but the same problem occurs.
I'm pretty stuck on this which I thought was going to be a simple quality of life improvement for the app.
Any suggestions would be gladly received.
See below for the relevent snippets of code from appDesigner.
TIA,
Mark.
properties (Access = public)
XLimOld = [0,1]; % placeholder
YLimOld = [0,1]; % placeholder
XLimTip = [0,1]; % placeholder
YLimTip = [0,1]; % placeholder
end
methods (Access = private)
% save previous plot zoom location to restore later.
function saveXYLimOld(app)
% only save current axis limits if zoom is manual and not equal to the
% tip position.
if all([strcmp(app.UIAxes.XLimMode,'manual'),...
app.UIAxes.XLim ~= app.XLimTip,...
app.UIAxes.YLim ~= app.YLimTip])
app.XLimOld = app.UIAxes.XLim;
app.YLimOld = app.UIAxes.YLim;
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ZoomTipButton
function ZoomTipButtonPushed(app, event)
% save previous plot zoom location to restore later.
saveXYLimOld(app)
% zoom to Tip location
app.UIAxes.XLim = app.XLimTip;
app.UIAxes.YLim = app.YLimTip;
end
% Button pushed function: ZoomOldButton
function ZoomOldButtonPushed(app, event)
% zoom to last manual zoomed location.
app.UIAxes.XLim = app.XLimOld;
app.UIAxes.YLim = app.YLimOld;
end
% Button pushed function: ZoomAutoButton
function ZoomAutoButtonPushed(app, event)
% save previous plot zoom location to restore later.
saveXYLimOld(app)
% reset zoom
app.UIAxes.XLimMode = 'auto';
app.UIAxes.YLimMode = 'auto';
end
end

Answers (1)

Praveen Reddy
Praveen Reddy on 15 Feb 2023
Edited: Praveen Reddy on 17 Feb 2023
Hi MC,
I Understand that you want to restore the last manually zoomed/panned axis limits. I have verified your code and checked that the buttons are working as expected. I reproduced a similar context and didn’t see any timing issues either. Please try updating your MATLAB to the latest version in case you face any issues.

5 Comments

Hi Praveen,
See below for an update.
I have updated to R2022b, but the same behaviour occurs.
See image below:
When debugging, the Zoom-Old functionality works. If running normally, it does not seem to redraw the axis, although it has correctly recalculated the major grid lines. No idea why it works for Zoom-Tip though which works in exactly the same way.
I think this may be something to do with being overconstrained. Both DataAspectRatio and PlotBoxDataAspectRatio are manully set to [1,1,1] to retain the square plot in the GUI and a real-world aspect ratio. The button callback then manually set the axis limits. So, if all these parameters don't produce a valid result it can't redraw???
I have tried letting one limit float (yLim([yLimOld(1), inf]), but then it jumps to the extents of the data which is not what I want.
Please try adding these aspect ratio constraints to your code to see if you can replicate the problem.
TIA,
Mark.
Hi MC,
I tried to reproduce the issue by manually setting “DataAspectRatio” to [1 1 1] and “PlotBoxAspectRatio” to [1 1 1]. The buttons are still working as expected for the data I have plotted for a sine curve. If possible please share the plot data.
Hi Praveen,
I tried to save to data but a popup says: "save not supported for matlab.app.AppBase objects".
This plot has 83 Line objects within app.UIAxes.Children.
I have also just tried setting
xtickformat(app.UIAxes,'%.2f');
ytickformat(app.UIAxes,'%.2f');
so that the plot bounding box does not change size.
This has not helped either.
MC
MC on 17 Feb 2023
Edited: MC on 17 Feb 2023
I have simplified the plot to just a sine wave.
The issue remains.
function ButtonPlotRefreshPushed(app, event)
x = 1:360;
y = 180*sind(x);
plot(app.UIAxes,x,y)
app.UIAxes.DataAspectRatio = [1 1 1];
app.UIAxes.PlotBoxAspectRatio = [1 1 1];
app.UIAxes.XGrid = 'on';
app.UIAxes.YGrid = 'on';
app.UIAxes.YAxisLocation = 'right';
app.UIAxes.TickDir = 'out';
xtickformat(app.UIAxes,'%.2f');
ytickformat(app.UIAxes,'%.2f');
if app.CheckBoxPlotFlipSag.Value
app.UIAxes.YDir = 'reverse';
else
app.UIAxes.YDir = 'normal';
end
app.UIAxes.XLabel.String = 'X: Radial Position, mm';
app.UIAxes.YLabel.String = 'Z: Sag, mm';
% save tip position.
tipRange = 1;
app.XLimTip = [90-tipRange, 90+tipRange];
app.YLimTip = [180-tipRange, 180+tipRange];
end

Sign in to comment.

Categories

Products

Release

R2022b

Asked:

MC
on 1 Feb 2023

Edited:

MC
on 17 Feb 2023

Community Treasure Hunt

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

Start Hunting!