Looping through different fields in struct

140 views (last 30 days)
I have a struct data field with many fields:
finalData.s7.bm5.rSync
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.
  2 Comments
Stephen23
Stephen23 on 26 Jul 2019
Edited: Stephen23 on 26 Jul 2019
"Is there an easy way to loop through all permutations of this data and save each loop as a descriptive variable?"
Yes, there are ways to do this.
"Any help is appreciated."
Do NOT do this. Putting numbers (or any meta-data) into variable names is a sign that you are doing something wrong. You should revise your code design.
Dynamically defining/accessing variables names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
"The first field is goes from s1, s2... s8, s9"
Then you should be using indexing, and not forcing meta-data (your index) into fieldnames or variable names. Indexing is simple, neat, and very efficient (unlike what you are trying to do).
You might like to consider using a table, which would make processing your data groups easy.
Guillaume
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.
fn=fieldnames(structure);
%loop through the fields
for i=1: numel(fn)
fn1=fieldnames(structure.(fn{i}))
for j=1: numel(fn1)
fn2=fieldnames(structure.(fn{i}).(fn1{j}));
for k=1: numel(fn2)
%access the data
data=structure.(fn{i}).(fn1{j}).(fn2{k});
end
end
end
  2 Comments
Guillaume
Guillaume on 30 Jul 2019
I'm sorry but no, if the field names are named as above they should be indexed. The code would then be:
%so structure is:
%finaldata.s(i).bm(j).rSync
%finaldata.s(i).bm(j).mSync
for i = 1:numel(finaldata.s)
for j = 1:numel(finaldata.s(i).bm)
rsync = finaldata.s(i).bm(j).rSync;
msync = finaldata.s(i).bm(j).mSync;
%... do something
end
end
which is so much easier to read. You could paper over the fundamental design issue with fieldnames, eval, etc. but this is only going to lead to more and more complex and unreadable code.
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)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!