How to iterate through structure fields

3 views (last 30 days)
I have a bunch of reaction time data (scalar) from subjects and am trying to figure out how best to vectorize my code. I have N subjects, each has Y conditions, and the number of values are different between subjects and between conditions. Also the maximum number of entries is unknown. My script loops through each subject, loading their data and searching for particular conditions and saving it. And I don't know how maThus, I can't simply have a matrix that is NxY .
The first thing I thought of is to put them all in a structure A with the conditions as different fields:
A(1:n).condition1
A(1:n).condition2
etc
But there are two problems: 1) I can't iterate through the fields, which is efficient and what I want to achieve, and 2) I can't easily obtain the average reaction times per condition from each subject. I would like to call mean(A(:).condition1) and have it output a 1xN array of the averages but it doesn't work that way.
What is the best way to contain data of variable length such that I can loop through to perform functions on the data? Thank you!

Accepted Answer

Star Strider
Star Strider on 10 Dec 2014
I would use a Cell Array. They require a bit of experimenting to learn about with them and use them efficiently, but they would seem to be the best option for your data.
An alternative could be Tables, so I would read about them as well.
  2 Comments
Kelly Steele
Kelly Steele on 12 Dec 2014
Yes the cell array helps get at that. I also realized I can make a large NaN matrix to fill in and then expand it if needed. Thanks.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!