绘制数字的数字不是图表数量的因素

时间:2015-01-08 16:18:57

标签: matlab sorting plot

我正在寻找一种从矩阵中平均绘制30列数据的方法,比如U数字,到目前为止,我有办法对30的因子做这个,但是我是现在很难让代码工作,比如U = 7.

代码需要在指定数量的数字U上尽可能平均地绘制数据列,每列数据代表不同的传感器,因此每个都是不同的图。

到目前为止(感谢Benoit_11)是:

ColPerFig = size(Data,2)/NumFigures; %// Number of columns to plot per figure

ColStart = 1:ColPerFig:size(Data,2) %// Indices of the starting columns to plot

%// Plot
for k = 1:NumFigures;    
hFig(k) = figure;        
plot(Data(:,ColStart(k):ColStart(k)+ColPerFig-1));
end

其中"数据"是4000x30和" NumFigures"的矩阵。是数字U.

如果有人有任何修改此代码的想法,所以它也适用于非因素30我会很感激。

由于

GibGib

1 个答案:

答案 0 :(得分:1)

如果您的列数不是您的数字的精确倍数,您必须决定如何处理其余列。

该提出的解决方案首先定义可以在每个图中绘制的列数。然后计算剩下的列数,然后在每个图中逐个展开这些剩余的列。

因此,在nFigure = 7的示例中,每个图可以有4列。我们仍然需要绘制2个额外的列,因此图1和图2实际上将绘制5列(而图3到7将只有4列)。

您可以轻松更改额外列的绘制位置(在最后一个上的第一个数字上)

%% // setup test data
Data = randi([9 11],100,30) ;
for ii=1:30
    Data(:,ii) = Data(:,ii)+(10*(ii-1));
end
NumFigures = 7 ;      %// this is the total number of figure

%% // calculate how many columns to plot per figure
nCol = size(Data,2) ;
nColPerFig = fix( nCol/NumFigures ) ;   %// Number of columns to plot per figure
nColRemain = mod(nCol,nColPerFig)  ;   %// Number of columns remaining to distribute on existing figures

ncol2plot = ones( NumFigures , 1 ) * nColPerFig ;       %// define how many columns will be plotted in each figure
ncol2plot(1:nColRemain) = ncol2plot(1:nColRemain)+1 ;   %// add a column per figure as many time as necessary to make sure we plot ALL columns

ColStart = cumsum([1;ncol2plot]) ;      %// Indices of the starting columns to plot
ColStart = ColStart(1:NumFigures) ;     %// truncate the vector in case it went too far

%% // Plot
for k = 1:NumFigures    
    hFig(k) = figure ;        
    plot( Data(:,ColStart(k):ColStart(k)+ncol2plot(k)-1) ) ;
    legend('show') %// this is just for a quick visualisation of how many columns we have in each figure
end