MATLAB Answers


Import joint angle vectors of varying sizes from 1x2826 struct (2826 trials) and output data into an array, where each vector is in a separate row.

Asked by Sebastian Skals on 16 Sep 2019
Latest activity Commented on by Raunak Gupta on 23 Sep 2019
I am trying to import joint angle data from a struct array containing 2826 trials of motion capture data (Xsens) using a for loop, and output the data into an array with 2826 rows each containing a specific joint angle vector. The vectors are varying sizes, as there are not the same number of frames in each trial.
If I only import one trial at a time it works fine:
for a = 1:length(Trials)
nSamples(a) = length(Trials(a).tree.subject.frames.frame);
if isfield(Trials(1).tree.subject.frames.frame(1),'jointAngle')
for b = 4:nSamples(1)
LeftKnee_FlexExt(b) = Trials(1).tree.subject.frames.frame(b).jointAngle(60);
RightKnee_FlexExt(b) = Trials(1).tree.subject.frames.frame(b).jointAngle(48);
I have tried to create a loop that imports all the 2826 trials to an array with rows of varying sizes, but I am stuck.
Thanks in advance for any assistance!


Sign in to comment.





2 Answers

Answer by Raunak Gupta on 19 Sep 2019
 Accepted Answer

Since for different rows of data in the structure, the size of vector is different So, arranging it into an array is not possible. If arranging it into an array is necessary, then you may append zeros or NaN or inf to the row at the end so that for each row its size become same. You may find the row with maximum size and fill other rows with default value at the end according to its size. While working on the data again you may read the rows from the array formed and remove these default values from the end.


Sign in to comment.

Answer by Sebastian Skals on 23 Sep 2019

Thank you for your response.
If I choose to append NaN or zeros to the array, wouldn't I still be left with the problem of not being able to perform operations on the complete array, as for instance, resampling the data to the same number of data points?
Any suggestion for how to proceed in this case?

  1 Comment

If you choose to append zeroes then the general matrix operations won't have effect but if you want to resample the data so that each row contains same number of points so you can do it while importing the struct into an array with some function that has a property of resampling. If it is a need to make an array, all rows must contains same number of columns that is why I suggest appending a number that won't effect other properties. If the data cannot be altered as mentioned it is suggested to keep it in struct format only as array formation won't be feasible.

Sign in to comment.