Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a structure within a structure with a dynamic name

I have large data sets which i want to work with in matlab.
I have a struct called Trail containing serveral structures called trail1, trail2 …
which then contain several matrices. I now want to add another point to for instance trail1
I can do that with Trail.trail1.a2rotated(i,:) = rotpoint'; the problem is that i have to do it in a loop where the trail number as well as the a2rotated changes to e.g. a3rot…

I tired to do it like that

name ="trail"+num2str(z)+".a2rotated"+"("+i+",:)"; 
name = convertStringsToChars(name);
Trail.(name) = rotpoint'

But that gives me the error: Invalid field name: ‘trail1.a2rotated(1,:)’.
Does someone have a solution?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The name in between brackets after the dot must be the name of a field of the struct. Other indexing operations must be done separately:

Trail.("trail"+z).a2rotated(i,:)

But you might be better off making trail(z) an array instead of separate fields with a number in the name.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading