function plot2(x)
hold on
fig = gcf();
ax = gca();
plot(ax, x)
fig.Visible = true;
figure('Visible', 'off')
hold off
end
為什么
>> plot2([1 2]); plot2([2 1])
創建兩個圖形,但是
>> plot2([1 2])
>> plot2([2 1])
一?我們有
plot2()<=> 使用 1 個現有圖形,創建 1 個新圖形>> plot2(); plot2()<=> 使用圖 X,創建圖 Y,使用圖 Y,創建圖 Z。>> plot2()>>plot2()<=> 使用圖 X,創建圖 Y。使用圖 Y?創建圖 Z?
disp(gcf)之前hold off顯示在這兩種情況下都在創建新的無花果,如Figure (25), Figure (26)。
uj5u.com熱心網友回復:
請注意,plot2([1 2]); drawnow(); plot2([2 1])在單行上呼叫使用與兩個單獨plot2呼叫相同的圖形。因此,在較高級別上,這可能是由于在沒有drawnow.
但是,除錯我們可以看到這是由于您的函式呼叫的操作順序有點奇怪plot2,該函式通過依賴gcf和使用對hold無序呼叫且未指定hold目標軸而對圖形進行模棱兩可的處理。
在下面的幾行中,我撰寫了一個代碼行,作為plot2給定命令呼叫期間的摘錄:
>> plot2([1 2]); plot2([2 1])
% Calling through "plot2([1 2])"
hold on % creates a figure and axes if one doesn't exist, or hold current
fig = gcf(); % gets the figure referenced by "hold"
ax = gca; % gets the axes referenced by "hold"
plot(ax,x) % plots on these axes
fig.Visible = true; % make the current figure visible
figure('Visible','off') % Create NEW figure, hidden. At this point you have 2 figures
hold off % does NOTHING, sets hold to "off" for new (invisible)
% figure which is the default anyway
% Calling through "plot2([2 1])"
hold on % holds "on" the CURRENT figure. This will depend whether the graphics buffer
% has been flushed. If not then the current figure is the hidden one created
% in the previous call, if it has then the current figure is the most recent
% visible figure. This is *not* specifically noted in the docs.
fig = gcf(); % gets the same figure referenced by "hold"
ax = gca; % gets the same axes referenced by "hold"
plot(ax,x) % plots on these axes
fig.Visible = true; % make the same "current" figure as above visible. When running with a
% graphics flush in between, this figure will already be visible
% due to the above behaviour
figure('Visible','off') % Create ANOTHER NEW figure, hidden.
% At this point you will have 3 figures!
hold off % does NOTHING, sets hold to "off" for new (invisible)
% figure which is the default anyway
您可以驗證我關于以 3 個圖形結束的最后評論,使用allFig = findall(0,'type','figure');它將顯示所有當前圖形物件,包括隱藏的圖形物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/507950.html
