首页 > 解决方案 > 试图将 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 个图表,当我尝试使用 subplot 制作一个新文件时,这些图表看起来完全不同。我究竟做错了什么?

图 1 是正确的图 图 2 是我试图将其放入子图中

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

标签: matlabplotsubplotfigure

解决方案


你需要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

推荐阅读