MATLAB Answers

Looping through different fields in struct

57 views (last 30 days)
I have a struct data field with many fields:
The first field is goes from s1, s2... s8, s9 and defines the stage of data collection.
The second field is the name of the subject (of which there are hundreds) and does not have a pattern.
The third field is a binary variable, defining when the responses of the subjects are synced. (rSync or mSync)
Is there an easy way to loop through all permutations of this data and save each loop as a descriptive variable? I have been doing this manually, but I would like to automate this process:
s7_bm5.rSync = finalData.s7.bm5.rSync
s7_bm5.mSync = finalData.s7.bm5.mSync
s8_bm5.rSync = finalData.s8.bm5.rSync
s8_bm5.mSync = finalData.s8.bm5.mSync
So I basically want to create variables based on struct file name and have a way to loop through all struct subfields.
Any help is appreciated.
Guillaume on 26 Jul 2019
Yes, you've gone wrong already. As Stephen said don't put metadata into variables names. Field names are variable names. The rule is simple if you start numbering variables, you've gone wrong.
x.s1, x.s2, x.s3, etc. should be x.s(1), x.s(2), x.s(3), This way it's trivial to access a field, you just use standard matlab indexing.
I agree that a table may be more suitable for your data. it would probably be a table with 4 variables (s, bm, rsync, msync)

Sign in to comment.

Accepted Answer

Dheeraj Singh
Dheeraj Singh on 30 Jul 2019
Edited: Dheeraj Singh on 30 Jul 2019
As mentioned in the comments above, it is not a good idea to define dynamic variable names.
But if you want to avoid manually writing each field name, you can use the fieldnames as shown below.
%loop through the fields
for i=1: numel(fn)
for j=1: numel(fn1)
for k=1: numel(fn2)
%access the data
Dheeraj Singh
Dheeraj Singh on 30 Jul 2019
Yeah sure,this can be a better design for the above mentioned problem but using fieldnames can be helpful in cases where we are want our field and subfield names to be more descriptive and we don't know beforehand the field names.

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!

Translated by