GibGib
GibGib

Reputation: 5

Plotting on figures where the number of figures isn't a factor of the number of plots

I'm looking for a way to plot 30 columns of data from a matrix equally on say, U number of figures, so far I have a way to to do this for factors of 30 however I'm now struggling to get the code to work for say U = 7.

The code needs to plot the columns of data as equally as possible over the specified number of figures U, each column of data represents a different sensor so each will be a different plot.

What I have so far (Thanks to Benoit_11) is:

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

Where "data" is the matrix of 4000x30 and "NumFigures" is the number of figures U.

If anyone has any ideas to modify this code so it also works for non factors of 30 I would appreciate it.

Thanks

GibGib

Upvotes: 0

Views: 43

Answers (1)

Hoki
Hoki

Reputation: 11812

If your number of column is not an exact multiple of your number of figure you have to decide what you do with the remaining columns.

This proposed solution first define the number of columns which can be plotted in each figure. It then count how many column were left out, then spread these remaining column one by one in each figure.

So in this example for nFigure=7, we can have 4 columns per figure. We still have to plot 2 additional columns, so figure 1 and figure 2 will actually plot 5 columns (and figure 3 to 7 will have only 4).

You can easily change where the extra columns will be plotted (on the first figures on on the last)

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

Upvotes: 1

Related Questions