1

Trying to find a way to use the Zoom in/out tools on a figure to automatically update fields in my app.

My main problem is that I need a event that triggers the fields to be updated. I could use a button to pull the new axis limits but I feel like there should be way around that. Does anybody know how I can detect a change in the figure axis without manually pulling update figure axis?

Turboman23
  • 21
  • 4

3 Answers3

1

Thanks to Oro777 for pointing me in the right direction. Here is the solution I came up with. After the figure I want this functionality applied to is created I add

z = zoom; z.ActionPostCallback = {@ZoomPostCallback,app}; z.Enable = 'on';

Where ZoomPostCallback is

function ZoomPostCallback(~,evd,app)
%Pull new x axes limits and apply them to app edit fields
xLim = evd.Axes.XLim;
app.TimeMinsEditField.Value = round(xLim(1),2);
app.TimeMaxsEditField.Value = round(xLim(2),2);

%Pull new y axes limits and apply them to app edit fields
yLim = evd.Axes.YLim;
app.FreqMinHzEditField.Value = round(yLim(1));
app.FreqMaxHzEditField.Value = round(yLim(2));

%Run changesAxes to ensure all other fields are updated
changeAxes(app)
end

It works really well. Hope this can help somebody else down the line. It all really hindges on the fact that zoom has the pre and post callback functionality.

Turboman23
  • 21
  • 4
1

I see you have added your own answer - but here is another way to do it, by adding listeners to the xlim and ylim properties of the axes:

hFig = figure;
ax = axes ( 'Parent', hFig );
% add the listeners - this will just display at the command line, but
%  hopefully you get the idea.
addlistener ( ax, 'XLim', 'PostSet', @(h,ev)disp ( 'xlim changed' ) )
addlistener ( ax, 'YLim', 'PostSet', @(h,ev)disp ( 'ylim changed' ) )
matlabgui
  • 5,642
  • 1
  • 12
  • 15
0

Are you using the axes from App Designer ? If so, the axes has not yet events as far as I know. It may change in the future.

For now, you could use the former axes object, it has much more settings and it will allow you to set Events as well. Check the MATLAB documentation for more information. https://www.mathworks.com/help/matlab/ref/zoom.html#brux2aq

Here is an example showing how to use the old axes object. In your startup function, you can add the object like below. Notice you must set the Parent setting else it will create another figure(not a uifigure).

axes('Parent', app.UIFigure)
oro777
  • 1,110
  • 1
  • 14
  • 29
  • 1
    Thanks for your response. I am not using axes from App Designer. Ran into too many problems early on when I was trying to figure out how it all worked and decided stick with making plots outside of App Designer. Ill dive into the link and see if I can figure it out. I've never used Parent settings before so this should be fun. – Turboman23 Jan 07 '20 at 15:53
  • Good choice, we should wait for Mathworks to finish their uiaxes development before to be able to use it fully. As I wrote, you can integrate the old axes inside a uifigure created by AppDesigner. – oro777 Jan 08 '20 at 02:41