尝试子图将8位数字放入2个窗口

问题描述

  x = [1 3 4 6 9];
    y = x + 3;

 stem(x); xlabel('My x axis'); ylabel('My y axis'); title('No title --:)'); 
 grid on;
 hold on;
stem(y);

我有8个使用这种格式制作的图形,我试图让它们出现在2个窗口中,每个窗口上有4个图形,当我尝试使用子图制作一个文件时,这些图形看起来完全不同。我在做什么错了?

图像1是正确的图形 图片2是我试图将其放入子图

x = [1 3 4 6 9];
y = x + 3;
subplot(2,2,1),plot(x,y)

解决方法

您需要2个循环。一个循环用于图形,另一个循环用于每个图形中的子图:

for i = 1:2
    figure; % this creates a separate figure
    
    for j=1:4
        subplot(2,2,j);
        x = randi(10,1,4);
        y = x + 3;
        stem(x); xlabel('My x axis'); ylabel('My y axis'); title('No title'); 
        grid on;
        hold on;
        stem(y);
    end
end