read multiple channels of different amounts of data into single matrix

4 views (last 30 days)
I have working code which reads different size data arrays from different files. Each array (matrix) originates from a different data "channel." Although there are differing amounts of data produced by each channel, each channel matrix has the same number of columns.
I want to combine all the data into a single array of data so it can be refernced by channel number. The resulting larger data matrix will thus have one additional column [compared to the channel data]; that column will be the channel index.
For example:
for ch = 1:10
chdata = ReadChData(ch);
% Add channel #ch data to larger matrix (DataMatrix) so that:
% chdata = DataMatrix(ch, ...)
% where the '...' would be replaced by the appropriate number of columns
end
I'm not sure how better to represent the general case so here's a specific example with 6 columns of data, for channel # nc:
chdata(:, :, :, :, :, :) = DataMatrix(nc, :, :, :, :, :, :)
(I do know or can determine the number of columns of data and it is consistent across channels. Each channel's data is read from a file in cell format and converted to a matrix, in case that makes a difference.)
  5 Comments
David Horton
David Horton on 23 Dec 2021
Sorry. I meant columns but defined the array by dimensions, not columns. The equation I wrote is rubbish!
To try and clarify: I receive N channels of data, each stored in a separate file. I read the data, line by line into an array. A line of data has M columns, which is determined by parsing a line and counting the spaces which separate columns - unless there is a space at the end of the line, the number of columns is the number of spaces plus one. Because of the format, it's read into a cell array and converted to a matrix. Headers at the top of the file define what the data in each column is. The number of columns and data definitions vary because data must be analyzed at different points in the process and analysis at different stages requires different data. Here is a sample line of data, with a header:
Record_ID Sample_# X_Position Y_Position Z_Position X_Speed Y_Speed Z_Speed Stage Status
195671 345 312.57 100.19 17.34 -7.01 3.12 0.23 17 9
Essentially, I want to merge all the data from all channels into one array, with an extra column designating which channel the data arrived in. For example, the above would simply become:
Channel Record_ID Sample_# X_Position Y_Position Z_Position X_Speed Y_Speed Z_Speed Stage Status
7 195671 345 312.57 100.19 17.34 -7.01 3.12 0.23 17 9
I see that you posted an answer below. I will see if it works ... I suspect it may.
David Horton
David Horton on 23 Dec 2021
Actually, I did originally want to add a dimension to the data, designating channel number so that, for example, the data for channel N could be extracted from the master list:
ChData = MasterArray(N, {:})
However, I see it's more efficient to add a column for channel number and concatenate the channel data matrices.
Because the amount of data (numbers of records) varies across channels, the master array would have to be sized to accomodate the largest number of records. As a result all the other channels would have differing numbers of blank records. By adding a column, I have no blank records and I access channel N data by finding all the records with channel number N in the specified column. (Boy did I over-complicate this one!)
Thanks for your help, Stephen.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 23 Dec 2021
Edited: Stephen23 on 23 Dec 2021
Assuming that all imported matrices have exactly the same number of columns in the same order:
N = 10;
C = cell(1,N)
for ch = 1:N
M = ReadChData(ch);
M(:,end+1) = ch;
C{k} = M;
end
A = vertcat(C{:})

More Answers (1)

David Horton
David Horton on 23 Dec 2021
Edited: David Horton on 23 Dec 2021

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!