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

조회 수: 23 (최근 30일)
KAE
KAE 2019년 4월 9일
편집: Andrew Peters 2022년 1월 27일
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?

채택된 답변

Dhanashree Mohite
Dhanashree Mohite 2019년 4월 12일
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.
  댓글 수: 2
KAE
KAE 2019년 4월 12일
편집: KAE 2019년 4월 12일
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
Benjamin Kraus
Benjamin Kraus 2020년 11월 19일
There is a bug with this solution that will cause some strange and unexpected behaviors.
The issue is the call to gca, which will only work if the axes has HandleVisibility set to 'on' and if the parent figure of the axes also has HandleVisibility set to 'on'. Otherwise, gca will create a new axes instead of operating on the axes you think it should. This is particularly troublesome for App Designer, because the figure has HandleVisibility set to 'off' by default.
The better way to achieve the same goal is to use a function handle and set the DefaultAxesCreateFcn like this:
set(groot, 'defaultAxesCreateFcn', @(ax,~) disableDefaultInteractivity(ax))
The difference here is that the axes handle itself is passed directly to disableDefaultInteractivity so it is no longer dependent upon gca working.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Bruno Luong
Bruno Luong 2019년 9월 14일
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
Bruno Luong
Bruno Luong 2019년 9월 17일
편집: Bruno Luong 2019년 9월 17일
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 2019년 9월 17일
Well I certainly appreciate that you shared what you tried! Sounds like a Yair Altman level question.

댓글을 달려면 로그인하십시오.


m miozzi
m miozzi 2020년 5월 22일
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 = [];

Andrew Peters
Andrew Peters 2022년 1월 27일
편집: Andrew Peters 2022년 1월 27일
disableDefaultInteractivity didn't work for me on R2021b, but putting this into startup did:
set(groot,'defaultAxesCreateFcn', @(ax,~) set(ax,'Interactions',[]));

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by