I set the default FigureNextplot property to 'new', as opposed to the factory default 'add':
set(groot, 'DefaultFigureNextplot', 'new')
When using hold on
Matlab sets the FigureNextplot and AxesNextplot properties to 'add', as it should. However, when I then finish working on the graph, hold off
resets the AxesNextplot property, but not the FigureNextplot property.
What is the reasoning behind this? Is there a way to keep using my default setting without ridding my codes of all hold
statements?
CodePudding user response:
You can use a workaround to prevent the figure's NextPlot
property from being changed:
- Intercept the call to
hold
by saving the following user-defined function in a file namedhold.m
, placing that file in a user folder and adding that folder to the path withaddpath
. - This user-defined function takes note of the figure's
NextPlot
property, calls the originalhold
function, and then restores the figure'sNextPlot
property.
function hold(varargin)
fnp = get(gcf, 'NextPlot'); % get figure's NextPlot property
w = which('hold' ,'-all'); % paths to the modified and original hold functions
dir = pwd; % take note of current folder
cd(fileparts(w{2})); % change to folder of original hold function
oh = @hold; % get a handle to that function
cd(dir) % restore folder
feval(oh, varargin{:}) % call original hold function
set(gcf, 'NextPlot', fnp); % set figure's NextPlot property to its previous value
Note that if you call hold
without arguments to toggle the hold state between on
and off
, the on
state is recognized as both the figure's and axis' NextPlot
properties having the values add
. This can be seen in the code of the original hold
function:
nexta = get(ax,'NextPlot');
nextf = get(fig,'NextPlot');
hold_state = strcmp(nexta,'add') && strcmp(nextf,'add');
Therefore, if the figure's NextPlot
property has the value New
, the hold
state is always considered to be off
, regardless of value of the axis' NextPlot
property. Thus a call to hold
without arguments always results in the plot being held.