2

I've organized some data into a nested structure that includes several subjects, 4-5 trials per subject, then identifying data like height, joint torque over a gait cycle, etc. So, for example:

subject(2).trial(4).torque

gives a matrix of joint torques for the 4th trial of subject 2, where the torque matrix columns represent degrees of freedom (hip, knee, etc.) and the rows represent time increments from 0 through 100% of a stride. What I want to do is take the mean of 5 trials for each degree of freedom and use that to represent the subject (for that degree of freedom). When I try to do it like this for the 1st degree of freedom:

for i = 2:24
    numTrialsThisSubject = size(subject(i).trial, 2);
    subject(i).torque = mean(subject(i).trial(1:numTrialsThisSubject).torque(:,1), 2);
end

I get this error:

??? Scalar index required for this type of multi-level indexing.

I know I can use a nested for loop to loop through the trials, store them in a temp matrix, then take the mean of the temp columns, but I'd like to avoid creating another variable for the temp matrix if I can. Is this possible?

dustynrobots
  • 311
  • 4
  • 8
  • 20
  • What happens if you give an index to `.torque`, i.e. `subject(i).torque(1)`? – darthbith Aug 08 '13 at 13:23
  • 1
    try using `struct2cell` you should have more flexibility with multi-level indexing.... – Shai Aug 08 '13 at 13:40
  • Nope unfortunately not darthbith - I get the same error if I use your suggestion, but thanks. And Shai - I'm not familiar with struct2cell or why that would help. Can you elaborate? – dustynrobots Aug 08 '13 at 17:20
  • @dustynrobots Is your question still relevant? Please consider marking the suggest answer as accepted, or revising it so that it can be answered properly. – Eitan T Sep 23 '13 at 21:04
  • 1
    @EitanT I answered my own question below based on a comment I made – dustynrobots Sep 24 '13 at 00:01

2 Answers2

1

You can use a combination of deal() and cell2mat().

Try this (use the built-in debugger to run through the code to see how it works):

for subject_k = 2:24
    % create temporary cell array for holding the matrices:
    temp_torques = cell(length(subject(subject_k).trial), 1);

    % deal the matrices from all the trials (copy to temp_torques):
    [temp_torques{:}] = deal(subject(subject_k).trial.torque);

    % convert to a matrix and concatenate all matrices over rows:
    temp_torques = cell2mat(temp_torques);

    % calculate mean of degree of freedom number 1 for all trials:
    subject(subject_k).torque = mean(temp_torques(:,1));
end

Notice that I use subject_k for the subject counter variable. Be careful with using i and j in MATLAB as names of variables, as they are already defined as 0 + 1.000i (complex number).

Community
  • 1
  • 1
Ole Thomsen Buus
  • 1,333
  • 1
  • 9
  • 24
  • Thanks! But I'm not as comfortable with deal and cell2mat, so I ended up just doing it like this: `for i = range numTrialsThisSubject = size(subject(i).trial, 2); for j = 1:numTrialsThisSubject subject(i).temp(:,j) = subject(i).trial(j).torque(:,1); end subject(i).torque = mean(subject(i).temp, 2); end' – dustynrobots Aug 08 '13 at 20:38
  • Okay :) your choice. I read your question as being how to achieve it without using an inner loop. There is no magic in my approach :) I suggest reading the docs and run the code line by line using the MATLAB debugger. This is the only sure to get comfortable. – Ole Thomsen Buus Aug 09 '13 at 00:25
  • Sorry I guess I should have phrased the question better - but thank you for rising to the challenge! I was trying to avoid a temp variable and another loop just because I thought the way I was trying would work but I had a syntax error I wasn't catching. But it seems like it just doesn't work that way, so adding another loop and temp variable was the next simplest execution. Thanks though! – dustynrobots Aug 09 '13 at 13:38
  • 1
    Did you try my approach? I am a bit interested in knowing if you got it working. Btw, the `deal` function is extremely useful if you want to extract data from structs. The syntax error is mainly because of MATLAB's limitations with data-structure lookup. You were trying to extract a single structure-element from multiple structs (the "trial(1:numTrialsThisSubject).torque(:,1)" part). This will be ambiguous to MATLAB and actually creates several returns. The deal-function is a way to get by this problem. – Ole Thomsen Buus Aug 09 '13 at 16:20
0

As mentioned above in my comment, adding another loop and temp variable turned out to be the simplest execution.

dustynrobots
  • 311
  • 4
  • 8
  • 20