为每列矩阵自动绘制图中的子图

时间:2013-09-26 16:24:24

标签: matlab plot matlab-figure

例如,假设我有一个带有colheaders的以下矩阵(< 9x6 double>)(< 1x6 cell>)。

 Matrix =

   226.7431   14.7437   14.9417   14.1000   14.5000       66.0590   
   226.7500   14.6582   14.8250       NaN   14.2000       66.7740   
   226.7569   14.3590   14.6067       NaN   13.9000       68.4897   
   226.7639   14.2702   14.5717   13.4000   13.8000       68.2487   
   226.7708   14.2555   14.6000       NaN   14.0000       NaN        
   226.7778   14.1605   14.5967       NaN   13.9000       NaN      
   226.7847   14.0320   14.4567   12.9000   13.6000       68.8272   
   226.7917   13.8422   14.2733       NaN   13.4000       69.6392   
   226.7986   13.6585   14.1169       NaN   13.1000       69.8048   

我想在x轴上绘制矩阵的第一列,其余的在y轴上绘制带有子图的matlab图(假设在一个图中为3)。手动我可以做这样的事情等等。

figure
subplot(3,1,1)
plot(Matrix(:,1),Matrix(:,2),'ro'); grid on; box on; xlabel('A');ylabel('B')
subplot(3,1,2)
plot(Matrix(:,1),Matrix(:,3),'bo'); grid on; box on; xlabel('A');ylabel('C')
subplot(3,1,3)
plot(Matrix(:,1),Matrix(:,4),'go'); grid on; box on; xlabel('A');ylabel('D')
and so on.....
......
......

现在开始一个棘手的部分,我需要像你这样的专家的帮助。我不想为我的矩阵进行手动绘图,因为它包含200列。所以我想要自动绘制矩阵,以便在子图中绘制矩阵的每一列。但是200个子图不能出现在一个数字中,因此它会在子图限制之后自动启动一个新的数字(比方说3)。除此之外,我还需要使用头文件'colheaders'自动定义'xlabel,ylabel,legend'。可能吗?

2 个答案:

答案 0 :(得分:3)

x = rand(10, 200);
myYLabel = char(64+randi(26, 200, 1));

nrows = 3;
ncols = 2;
for ii = 1:size(x, 2)
    if nrows*ncols-mod(-ii, nrows*ncols) == 1
        figure;
    end
    subplot(nrows, ncols, nrows*ncols-mod(-ii, nrows*ncols));
    plot(x(:, ii));
    ylabel(myYLabel(ii, :));
end

答案 1 :(得分:1)

这似乎是一个简单的循环任务。

我假设你知道每个图中你想要多少个子图(让我们说3)。

A = yourdatamatrix;   
header = [{'A'}, {'B'}, {'C'}, {'D'}, {'E'}, {'F'}];
n = 6 %number of columns

i_figure = 1;


for ii=1:3:n-3
    figure(ii)
    subplot(3,1,1)
    plot(A(:,1),A(:,ii+1),'ro'); grid on; box on; xlabel(header(1));ylabel(header(ii+1))
    subplot(3,1,2)
    plot(A(:,1),A(:,ii+2),'bo'); grid on; box on; xlabel(header(1));ylabel(header(ii+2))
    subplot(3,1,3)
    plot(A(:,1),A(:,ii+3),'go'); grid on; box on; xlabel(header(1));ylabel(header(ii+3))
end

我假设您不关心您的图号,否则只是实施另一个计数器。 另请注意,列+ 1的数量可以被3整除。