I have a MATLAB matrix which has 4 columns and 290,000 rows. Every 5000 rows I have new data that I want to plot separately from the rest of the data. How do I split the matrix that has 290,000 rows into matrices/blocks of 5,000 rows and plot those matrices?
1 Answer
You have a few options here which come down to - do you want to plot all of the data as one line or separate lines?
Option 1
Use a loop to plot every episode of 5000 rows. Here I'm assuming you're plotting the first column only. For example:
axesHandle = axes;
axesHandle.NextPlot = 'add';
totalRowCount = 290000;
episodeRowCount = 5000;
for ii = 1:episodeRowCount:totalRowCount
startRowIndex = ii;
endRowIndex = startRowIndex + episodeRowCount - 1;
if endRowIndex > totalRowCount
endRowIndex = totalRowCount;
end
plot( YOURDATAMATRIX(startRowIndex:endRowIndex,1), 'Parent', axesHandle );
end
Option 2
Insert a row of NaN values after each episode of 5000 records and then plot the entire collection of episodes at once. Again I'm assuming you're plotting the first column only. The trick here is to work backwards from the end of the matrix when inserting rows so that the location where you need to insert the next row is not affected by the inserts already done:
totalRowCount = 290000;
episodeRowCount = 5000;
lastInsertRowIndex = floor( totalRowCount / episodeRowCount ) * episodeRowCount;
for ii = lastInsertRowIndex:-episodeRowCount:1
insertRowIndex = ii + 1;
YOURDATAMATRIX = vertcat( ...
YOURDATAMATRIX(1:insertRowIndex,:), ...
[ NaN NaN NaN NaN ], ...
YOURDATAMATRIX(insertRowIndex:end,:) );
end
plot( YOURDATAMATRIX(startRowIndex:endRowIndex,1) );
Option 3
Reshape the column that you want to plot into a 5000 x N matrix and then use this new matrix in the plot command. For example:
reshapedFirstColumn = reshape( YOURDATAMATRIX(:,1), 5000, 290000 / 5000 );
plot( reshapedFirstColumn );