如何在MATLAB中的for循环内切换不同数字的子图

时间:2015-02-12 22:56:04

标签: matlab matlab-figure subplot

请考虑以下代码段

%% Declare figures
figure(1); % Plot Measured (y) Vs Reference(x) data for 6 cases
figure(2); % Plot Regression fit for Measured vs Reference for 6 cases

%% Run algorithm for 6 cases
for i=1:6
:
:
subplot(3,2,i);plot(x,y); % should go to figure 1
:
linearfittype = fittype({'0','x','1'});
f = fit(f,x,y);
subplot(3,2,i);plot(f,x,y); % should go to figure 2
end

如何将子图分配到适当的数字?

1 个答案:

答案 0 :(得分:2)

如果我理解正确,只需在figure(1)声明之前写figure(2)subplot即可。

  

如果h是现有数字的句柄或数字属性值,则figure(h)使现有数字成为当前数字,使其可见,并将其移到所有其他数字之上屏幕。当前数字是图形输出的目标。

所以:

%% Declare figures
figure(1); % Plot Measured (y) Vs Reference(x) data for 6 cases
figure(2); % Plot Regression fit for Measured vs Reference for 6 cases

%% Run algorithm for 6 cases
for i=1:6
:
:
figure(1) %// make figure 1 the current figure
subplot(3,2,i);plot(x,y); %// should go to figure 1
:
linearfittype = fittype({'0','x','1'});
f = fit(f,x,y);
figure(2) %// make figure 2 the current figure
subplot(3,2,i);plot(f,x,y); %// should go to figure 2
end