pan in a single axes

2 Ansichten (letzte 30 Tage)
Dmitry Kaplan
Dmitry Kaplan am 15 Jun. 2022
Beantwortet: Karan Singh am 4 Okt. 2023
I have a figure with 21 axes.
I need to turn the horizontal pan on within a single axes with a dead-simple callback when pan is executed:
So I do this:
h = pan;
setAllowAxesPan(h,ax_target,true);
h.Motion = 'horizontal';
h.ActionPostCallback = {@post_pan_callback, h, ax_target};
h.enable = 'on';
This turns on the blue/grey pan thingy (axtoolbar) and the callback works when I drag the curve left and right.
BUT
(because I completely fail to understand the interaction between the pan object and the good-old pan)...
problem 1
Even if I disable the pan thingy, I can still pan and the callback doesn't execute. I want the ability for the pan to be under user control. When the thingy is off, pan should be off. What the heck is the point of it then? If the callback doesn't execute, wrong data are displayed.
problem 2
The pan thingy infects the other 20 axes because h is assigned to a figure, not an axes. WHY? Can I have mutlple pan objects, one per axes?
As it stands now, I need to turn off the pan thingy on the ax_target on in order to interact with the other axes. -- you know, the ones that should NOT pan under any circumstances because it tooks hours of work to align them.
problem 3
I want to turn off the panning behavior globally if I click outside of ax_target. IE when I click on one of the other axes, I want the pan on ax_target to turn the heck off. When I try to set
if ~(strcmp(eventdata.Axes.Tag,'the axes that I want the pan to work in')), h.enable='off';
inside the callback, I get the typical matlab goobledeegook below, when I try the good old "pan('off')" inside the callback and I get the same thing:
Warning: An error occurred during the mode callback.
In matlab.uitools.internal.uimode/fireActionPostCallback (line 16)
In pan>locWindowButtonUpFcn (line 375)
In pan>@(~,e)locWindowButtonUpFcn(fig,e,ax,hMode,buttonDownData) (line 541)
In hgfeval (line 62)
In matlab.uitools.internal.uimode/modeWindowButtonUpFcn (line 52)
In matlab.uitools.internal.uimode/setCallbackFcn>localModeWindowButtonUpFcn (line 56)
I think that figure's properties are blocked during the pan callback. So what should I do? Keep global variables?

Antworten (1)

Karan Singh
Karan Singh am 4 Okt. 2023
Hi Dmitry,
From what I understand, the goal is to enable horizontal panning within a specific axis, trigger a callback function when panning occurs, have separate pan functionality for each axis, and turn off panning for the target axis when clicking on other axes. 
To address the issues you are facing with the pan functionality in MATLAB, let's go through each problem and discuss potential solutions:
Problem 1: By default, turning off the toolbar does not disable the pan functionality. To achieve the desired behaviour, you can manually handle the enable/disable state of the pan object based on the toolbar's status. You can add a listener to the toolbar's State property and update the pan object accordingly. Here's an example:
h = pan;
setAllowAxesPan(h, ax_target, true);
h.Motion = 'horizontal';
h.ActionPostCallback = {@post_pan_callback, h, ax_target};
toolbar = ax_target.Parent.Toolbar;
stateListener = addlistener(toolbar, 'State', 'PostSet', @(~,~) updatePanState(h, toolbar));
% Custom function to update the pan object's enable state
function updatePanState(panObject, toolbar)
if strcmp(toolbar.State, 'off')
panObject.Enable = 'off';
else
panObject.Enable = 'on';
end
end
Problem 2: The pan object in MATLAB is designed to work at the figure level, affecting all axes within that figure. To have separate pan functionality for each axis, you can create separate pan objects for each axis individually. Here's an example:
h1 = pan(ax1);
setAllowAxesPan(h1, ax1, true);
h1.Motion = 'horizontal';
h1.ActionPostCallback = {@post_pan_callback, h1, ax1};
h2 = pan(ax2);
setAllowAxesPan(h2, ax2, true);
h2.Motion = 'horizontal';
h2.ActionPostCallback = {@post_pan_callback, h2, ax2};
% Repeat for other axes...
Problem 3: To disable panning for the target axis when clicking on other axes, you can use the HitTest property of the axes. Set the HitTest property of the non-target axes to off so that they don't respond to mouse clicks. Here's an example:
% Set 'HitTest' property to 'off' for non-target axes
set(ax1, 'HitTest', 'off');
set(ax2, 'HitTest', 'off');
% Repeat for other axes...
% Add a callback to the figure to handle mouse clicks
set(fig, 'WindowButtonDownFcn', @figureClickCallback);
% Custom function to handle mouse clicks on the figure
function figureClickCallback(~, eventdata)
clickedAxes = eventdata.Source.CurrentAxes;
if clickedAxes ~= ax_target
h.enable = 'off';
end
end
Attached below are some documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by