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

How to switch between subplots of different figures inside a for loop in MATLAB

考虑以下代码片段,

%% 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

如何将子图分配给适当的图形?

如果我没理解错,在subplot语句前写figure(1)figure(2)即可。

If h is the handle or the Number property value of an existing figure, then figure(h) makes that existing figure the current figure, makes it visible, and moves it on top of all other figures on the screen. The current figure is the target for graphics output.

所以:

%% 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