How to extract data stored in a matrix and in a specific range of columns whose number is stored in another matrix?

1 view (last 30 days)
MD on 6 Oct 2021
Commented: dpb on 6 Oct 2021
Hi, I am new to Matlab and I would be grateful if you could give me a hand.
I have several waveform data from various participants in a matrix (in raws). This is how the data look like in Matrix1 (which does not have the text at the beginning of the raw, I added it to simplify the explanation):
'ParticipantID' 202 202 202 [...] 203 203 203 [...]
'Stage' 0 0 0 [...] 0 0 0 [...]
'Frame' 1 2 3 [...] 1 2 3 [...]
'flexion' 69 68 69 [...] 72 73 74 [...]
'ext/int rotation' -14 -13 -11 [...] -15 -15 -16 [...]
'abd/adduction' 5 4 0 [...] 2 3 2 [...]
'ml shift' -7 -7 -7 [...] -4 -4 -5 [...]
'ant/post draw' 14 15 16 [...] 16 18 19 [...]
'distal comp' 25 25 25 [...] 20 20 20 [...]
As you can see, the waveform data for different participants come in succession.
I have another matrix (Matrix2) with one raw only. The numbers refer to the column number in Matrix1 where the data for a different participant start. Matrix2 looks like this:
1 221 463 675 903 1096 1367 1574 [...] 3888
This means that data for participantID 202 range from column 1 up to column 220 of Matrix1, participantID 203 data start from column 221 up to column 462, etc.
I would like the code to pick the data for each participant, separately. I wrote a for loop:
for i = 1:size(Matrix2)
B1 = Matrix2(1, i);
for k = 2:size(Matrix2)
B2 = Matrix2(1, k);
EachPatientData= Matrix1(:, B1:B2-1);
I do not get any error message when I run the code but obviously I cannot see what the loop is doing and I think I am only getting the last data displayed in the workspace, so B1=3888, B2=3888 and EachPatientData is empty (no surprise here).
Is the procedure I am following right?
How can I see the data extracted with each of the loop iterations to make sure I am going in the right direction?
Many thanks!

Answers (1)

dpb on 6 Oct 2021
It would be much simpler if you would use a table for each variable and include the ParticipantID as a variable instead.
However, unless the time series are the same length for each, you can't use a table to hold all the data together unless you use a missing value indicator for the shorter series as arrays in MATLAB must be rectangular. The alternative is a cell array which can hold arrays of different lengths in different cells.
However, given the stucture you have, the simpler way to pull the information is to use logical indexing/addressing on the ID in the data array itself; this way you can eliminate the second indexing array.
uID=unique(M(1,:)); % get the unique IDs (M is shorthand for your Matrix1)
for i=1:numel(uID) % iterate over the list of IDs
P{i}=M(ismember(M(1,:),uID(i)),4:end); % extract 4th and subsequent rows for each column
dpb on 6 Oct 2021
Attach the M1 array as a .mat file so we can test something out instead of just air code...or at least show us the code in its entirety including the error message; we can't debug what we can't see.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by