How to iterate through structure fields

4 visualizaciones (últimos 30 días)
Kelly Steele
Kelly Steele el 10 de Dic. de 2014
Comentada: Star Strider el 12 de Dic. de 2014
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!

Respuesta aceptada

Star Strider
Star Strider el 10 de Dic. de 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 comentarios
Kelly Steele
Kelly Steele el 12 de Dic. de 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.
Star Strider
Star Strider el 12 de Dic. de 2014
My pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by