matlab:当“add”不是默认值时,“hold off”语句会永久更改FigureNextplot属性

Chr*_*tie 4 matlab matlab-figure

我将默认的FigureNextplot属性设置为“new”,而不是工厂默认的“add”:

set(groot, 'DefaultFigureNextplot', 'new')
Run Code Online (Sandbox Code Playgroud)

使用hold onMatlab 时,应将FigureNextplot 和 AxesNextplot 属性设置为“add”。但是,当我完成图形处理时,hold off会重置 AxesNextplot 属性,但不会重置 FigureNextplot 属性。

这背后的原因是什么?有没有办法继续使用我的默认设置而不删除我的代码中的所有hold语句?

Lui*_*ndo 5

NextPlot您可以使用解决方法来防止更改图窗的属性:

  • 通过将hold以下用户定义函数保存在名为 的文件中hold.m、将该文件放置在用户文件夹中并将该文件夹添加到带有 的路径来拦截对 的调用addpath
  • 此用户定义的函数记录图窗的NextPlot属性,调用原始hold函数,然后恢复图窗的NextPlot属性。
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
Run Code Online (Sandbox Code Playgroud)

请注意,如果您hold 不带参数on调用以在 和之间切换保持状态off,则该on状态将被识别为图窗和轴的NextPlot属性都具有值add。这可以在原函数的代码中看到hold

nexta = get(ax,'NextPlot');
nextf = get(fig,'NextPlot');
hold_state = strcmp(nexta,'add') && strcmp(nextf,'add');
Run Code Online (Sandbox Code Playgroud)

因此,如果图窗的NextPlot属性值为New,则hold状态始终被视为off,而不管轴NextPlot属性的值如何。因此,不带参数的调用hold总是会导致情节被保留。