Zoom Triggered changes in a plot

I have a long signal with several annotations. I'd like the annotations to be visible only when I zoom in. Is there a way to do that?
I've tried using a callback function with 'ActionPostCallback':
figure()
%plot the signal
plot(t,fsig);
hold on;
h = zoom;
h.ActionPostCallback = @showpks;
h.Enable = 'on';
function showpks(obj,evd,s)
%mark the peaks
plot (t(pks), fsig(pks), '*');
end
but I'm getting the following warning messege:
Warning: An error occurred during the mode callback.
> In matlab.uitools.internal.uimode/fireActionPostCallback (line 16)
In zoom>local2DButtonUpFcn (line 1383)
In zoom>@(o,e)local2DButtonUpFcn(o,e,hMode,buttonDownData) (line 1134)
In hgfeval (line 62)
In matlab.uitools.internal.uimode/modeWindowButtonUpFcn (line 52)
In matlab.uitools.internal.uimode/setCallbackFcn>localModeWindowButtonUpFcn (line 56)
Is there another way of triggering the appearance of markers when zooming in?

5 Comments

Adam
Adam on 16 Oct 2019
Edited: Adam on 16 Oct 2019
Depending how that function was defined (i.e. if it isn't a nested function) I imagine the error was that it doesn't know what t, pks and fsig are.
You should be able to do it in the ActionPostCallback, but don't do it that way. I would suggest you show all the annotations and change their visibility in the callback, or keep a variable for each of them and make it empty if it is not to be shown and plot only if it is empty. Otherwise, with what you have there, if it did work, you would keep plotting annotations over the top of each other constantly when you zoom, without ever deleting any of them.
Alternatively I guess the zoom callback can check all the currently plotted ones and delete any that are no longer in view. Either way you need to delete them too, not just keep plotting new ones.
Hi Adam, thank you for the quick response.
I've tried changing the visibility using 'set' within the callback but I'm getting the same warning.
Perhaps I'm not using the callback function correctly.
Could you give me an example of how it can be done?
After adding input variables to the callback function, I'm still unable to plot within the callback function.
Currently I'm trying:
figure();
plot(t,fsig);
hold on;
h = zoom;
h.Enable = 'on';
h.ActionPostCallback = @showpks;
function showpks(h,evd,t,pks,fsig)
X_Lim = evd.Axes.XLim;
fig = gcf;
a = findobj(fig,'type','Axes');
if diff(X_Lim)<=3600 %one hour
set(a,'NextPlot','add');
plot(a,t(pks),fsig(pks),'*');
end
end
but am still getting the warning.
How can I add the data I want to the exisiting figure, from inside the callback function?
Adam
Adam on 17 Oct 2019
Edited: Adam on 17 Oct 2019
h.ActionPostCallback = @showpks;
needs to include the variables you are passing in. I can't remember off the top of my head what arguments that function takes as default, but most callbacks take the source and event data as first two arguments. Assuming they are your h and evd then
h.ActionPostCallback = @(h,evd) showpks( h, evd, t, pks, fsig );
should work as the callback syntax, though there are other syntaxes too.
Thank you!

Sign in to comment.

Answers (0)

Categories

Asked:

on 16 Oct 2019

Edited:

on 17 Oct 2019

Community Treasure Hunt

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

Start Hunting!