Hi Tobias,
I understand that you're trying to set 'axis equal' on a UIAxes in App Designer to maintain the correct aspect ratio, but when you zoom using the built-in toolbar, the axes change in a way that's different from the behavior in a standard MATLAB figure.
This happens because 'UIAxes' object behaves slightly differently from standard axes. In case of App Designer, 'axis equal' is integrated initially, but interactive zooming and panning can override the equal scaling due to layout management and resizing behavior.
To maintain an equal aspect ratio even after zooming, you can use the 'DataAspectRatio' property instead of relying solely on 'axis equal'. Here's how you can utilize the same in a MATLAB application:
app.UIAxes.DataAspectRatio = [1 1 1];
This tells the axes to preserve the ratio of data units in x, y, and z directions, which is equivalent to 'axis equal' and persists even during zooming or panning.
However, in MATLAB R2020b, UIAxes can still behave unexpectedly when constrained by the App layout system. To improve interactivity:
- You can disable some automatic resizing by anchoring the 'UIAxes' size or placing it inside a fixed-size panel.
- Alternatively, you can manually reset the aspect ratio after every zoom/pan interaction using callbacks.
For automating the above mentioned process, you can link a listener to the 'UIAxes' limits, as shown below:
addlistener(app.UIAxes, 'XLim', 'PostSet', @(src, evt) enforceAspectRatio(app));
addlistener(app.UIAxes, 'YLim', 'PostSet', @(src, evt) enforceAspectRatio(app));
function enforceAspectRatio(app)
app.UIAxes.DataAspectRatio = [1 1 1];
This approach ensures that even after zooming, the aspect ratio is re-applied consistently.
For more information regarding various function and parameters mentioned in the given code snippet, you can refer to the following documentation link:
Best!