How to call xlim and ylim after the most recent zoom?

12 次查看(过去 30 天)
I am working on a small program and would like to realize the following feature:
After zooming in/out, I want to store the current (the most recent one) xlim and ylim values, which can be used in another function.
I was trying to use 'ActionPostCallback' but it was not successful. Any idea that how I should approach this? Even some pseudo codes will be much appreciated.
Thank you in advance.

采纳的回答

Luna
Luna 2019-9-5
编辑:Luna 2019-9-5
Hello,
Try this:
hFig = figure;
hAxis = axes(hFig);
hPlt = plot(hAxis,rand(100,1));
zoomObj = zoom(hFig);
set(zoomObj,'ActionPostCallback', @zoomChanged);
function zoomChanged(~,AxisStruct)
xLimValues = get(AxisStruct.Axes,'XLim'); % you may want to save that into a handles struct of your gui to access it from another function
yLimValues = get(AxisStruct.Axes,'YLim');
end
Or you may want a class definition for that to get variables outside. Here:
classdef zoomChangeEvent < handle
properties
xLims
yLims
end
methods
function obj = zoomChangeEvent(obj)
hFig = figure;
hAxis = axes(hFig);
hPlt = plot(hAxis,rand(100,1));
zoomObj = zoom(hFig);
set(zoomObj,'ActionPostCallback', @obj.zoomChanged);
end
function zoomChanged(obj,~,AxisStruct)
obj.xLims = get(AxisStruct.Axes,'XLim') % you may want to save that into a handles struct of your gui to access it from another function
obj.yLims = get(AxisStruct.Axes,'YLim')
end
end
end
Just save the function and call it like below:
myZoomEvent = zoomChangeEvent
While constructing the object myZoomEvent, it will plot you some random data. Just play around with cursor and see it will print you the object's xLim and yLim properties.
The main difference between 1st and 2nd solution:
Both works when you clicked on it. Since you can't define a return value on callback functions, 1st solution tells you that: "What happens inside the function, stays inside the function."
On the other hand 2nd solution passes the object reference into zoomChanged callback function. So you can access the values via object properties you have assigned with dot notation.
Hope those will be helpful :)
  2 个评论
Edward Huang
Edward Huang 2019-9-6
编辑:Edward Huang 2019-9-6
Thanks! My attempt was actually quite similar to your first approach, but the actionpostcallback seemed to fail to capture the values resulting xlim/ylim as undefined variables. I have tried your code and it works as a standalone method. I guess my issue involves the calling of those values from another function as well.
Thanks again!
Luna
Luna 2019-9-9
Your welcome :)
If you want to access xlim/ylim values from another function, you should assign those values to an object property. This way you can reach xlim and ylim from another function.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by