MATLAB Answers

How to set disableDef​aultIntera​ctivity for all axes?

67 views (last 30 days)
KAE
KAE on 9 Apr 2019
Answered: m miozzi on 22 May 2020 at 16:00
In Matlab 2018b and later, the cursor is set to automatically interact with the plot. This messes up my plots (accidentally moving axes or adding unwanted data tips), so I would like to turn it off. You can do this for a specific axis ax with disableDefaultInteractivity(ax). However I would like to apply this to all axes I ever make. I know you can set preferences for some axis properties as follows,
set(0,'DefaultAxesColor', 'none');
But the following attemps did not work,
disableDefaultInteractivity(0) % Error: Input must be an axes handle
set(0, 'DefaultInteractivity', 'off') % Error: Too many output arguments
Is there another way to disable the axis interactivity for all axes?

  0 Comments

Sign in to comment.

Accepted Answer

Dhanashree Mohite
Dhanashree Mohite on 12 Apr 2019
In order to disable default interactivity for all created axes, you can use the following command:
>> set(groot,'defaultAxesCreateFcn','disableDefaultInteractivity(gca)')
But this setting will be reset when MATLAB is restarted.

  1 Comment

KAE
KAE on 12 Apr 2019
I put it in my startup file, so I can always have this set. Thanks so much - I would never have worked out this groot formatting. Since I also run older versions of Matlab, which don't have this setting, I had to implement it in my startup.m as follows,
a = version; % Get the version of Matlab that is running
if ~isempty(findstr(a, '2019')) % Only apply to R2019
set(groot,'defaultAxesCreateFcn','disableDefaultInteractivity(gca)'); % Disable interactivity of cursor in axes
end

Sign in to comment.

More Answers (2)

Bruno Luong
Bruno Luong on 14 Sep 2019
For R2019b all the tricks found previously no longer work.
I found this one does the trick
set(groot,'defaultAxesCreateFcn', ...
@(ax,varargin) start(timer('StartDelay',1,'ExecutionMode','singleShot','TimerFcn',@(varargin) axtoolbar(ax,{}))));
You might put it in startup.m file
if ~verLessThan('MATLAB','9.7')
set(groot,'defaultAxesCreateFcn', ...
@(ax,varargin) start(timer('StartDelay',1,'ExecutionMode','singleShot','TimerFcn',@(varargin) axtoolbar(ax,{}))));
end

  4 Comments

Show 1 older comment
Bruno Luong
Bruno Luong on 16 Sep 2019
I have a more robust and cleaner version, that does not create plenty of timer objects
You can call
DisableAxeToolbar() in the startup.m (need to be on the default path) or independently.
function DisableAxeToolbar()
%
try %#ok
if ~verLessThan('matlab','9.5')
set(groot,'defaultFigureCreateFcn',@(fig,dummy)addToolbarExplorationButtons(fig));
if verLessThan('MATLAB','9.7')
set(groot,'defaultAxesCreateFcn',@(ax,dummy)set(ax.Toolbar,'Visible','off'));
else
set(groot,'defaultAxesCreateFcn', @(ax,varargin) TriggerAxeToolbarRemovalTimer([], ax));
end
fprintf('Restore global figure Toolbar\n');
end
end
end
%%
function TriggerAxeToolbarRemovalTimer(th, varargin) % R2019 or later
persistent T
if isempty(th)
if isempty(T)
T = CreateAxeToolbarRemovalTimer();
end
th = T;
end
if nargin >= 2
ax = varargin{1};
try
set(th,'UserData',ax);
start(th);
catch
% if we get here many axes are being created, we stack the timer
newth = CreateAxeToolbarRemovalTimer('StopFcn', @(obj, varargin) delete(obj));
TriggerAxeToolbarRemovalTimer(newth, varargin{:});
end
end
end % TriggerAxeToolbarRemovalTimer
function T = CreateAxeToolbarRemovalTimer(varargin)
T = timer('Name','AxeToolbarRemovalTimer',...
'BusyMode', 'queue', ...
'StartDelay',1,...
'ExecutionMode','singleShot',...
'TimerFcn',@(obj, event, varargin) Disableaxtoolbar(get(obj,'UserData')),... % get(obj,'UserData') returns axe handle
varargin{:});
end
function Disableaxtoolbar(ax)
try %#ok
axtoolbar(ax,{});
% fprintf('disable toolbar axe invoked\n')
end
end
Bruno Luong
Bruno Luong on 17 Sep 2019
Limitation: when new-plot replaces the oldone, the toolbar comeback !
close all
plot(rand(1,10)); % toolbar removed
hold on
plot(rand(1,10)); % toolbar still dsappears
hold off
plot(rand(1,10)); % toolbar comes back
% But when I call this it disappears again !!!!
axtoolbar(gca)
I don't quite understand how the toolbar works.
Right now I don't know how to work around this.
KAE
KAE on 17 Sep 2019
Well I certainly appreciate that you shared what you tried! Sounds like a Yair Altman level question.

Sign in to comment.


m miozzi
m miozzi on 22 May 2020 at 16:00
Yes, Yair!!
In http://undocumentedmatlab.com/articles/improving-graphics-interactivity there is a suggestion about disabling unwanted interactivity behaviors while preserving zoom and pan (but you can choose what you like...):
hAxes.Interactions = [zoomInteraction regionZoomInteraction rulerPanInteraction];
hAxes.Toolbar = [];
The first attempts to apply the suggestion failed like described by Bruno, because of the reset of the default axes Interaction property at line 53 of clo.m (<MATLABROOT>\toolbox\matlab\graphics\private\clo.m).
By adding lines 54 and 55 in clo.m (see below) everything works like requested.
This is encouraging, but a serious analysis of the side effects was not done yet.
52 obj = handle(obj); % In case of double handle
53 obj.clo(hsave, (do_reset == 1)); % Call clo method on graphics class
54 obj.Interactions = [zoomInteraction regionZoomInteraction rulerPanInteraction];
55 obj.Toolbar = [];

  0 Comments

Sign in to comment.

Sign in to answer this question.