user7431005
user7431005

Reputation: 4539

matlab guide, emit event (or signal) when zoom

I have a GUI in Matlab created with guide. Inside this GUI is a axes. I want to emit a signal as soon as someone is changing the zoom of this plot.

Why:

I need to change the XTickLabels. When zooming in the XTick change and I need to update the XTickLabels. It is not possible to hard code the XTick values (because you should be able to zoom in and get better fitting XTick values).

In C++ with Qt I would simply emit a signal as soon as some zoom factor changes and connect it to a slot who changes the XTickLables. I'm not sure how to do it with MATLAB.

Upvotes: 0

Views: 163

Answers (1)

Hoki
Hoki

Reputation: 11792

The link in my comment explains how to attach a listener to any (Observable) property change, but if you're only interested in events triggered by zooming action, you can get events fired directly by the zoom object.

Below is a small demo on how to attach event handlers to the zoom object:

function demozoomevent
% Listen to zoom events

    % Sample figure and plot
    plot(1:10);
    % retrieve the zoom object handle
    h = zoom;
    % set the callback for 'before' and 'after' zoom event
    h.ActionPreCallback = @myprecallback;
    h.ActionPostCallback = @mypostcallback;
    % Activate the zoom
    h.Enable = 'on';


function myprecallback(obj,evd)
% will be executed BEFORE the zooming happens
    disp('A zoom is about to occur.');

function mypostcallback(obj,evd)
% will be executed AFTER the zooming happended
    newLim = evd.Axes.XLim;
    msgbox(sprintf('The new X-Limits are [%.2f %.2f].',newLim));

note: This way does not listen to any change of XLim or associated XTickLabel, but only to the zoom events. If you want to use that method, consider doing the same thing with the pan object, as it could also be used to change the XLim of your axes.

Upvotes: 1

Related Questions