How to iterate through structure fields

조회 수: 6 (최근 30일)
Kelly Steele
Kelly Steele 2014년 12월 10일
댓글: Star Strider 2014년 12월 12일
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!

채택된 답변

Star Strider
Star Strider 2014년 12월 10일
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
Kelly Steele
Kelly Steele 2014년 12월 12일
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.
Star Strider
Star Strider 2014년 12월 12일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by