Home > front end >  Creating a structure within a structure with a dynamic name
Creating a structure within a structure with a dynamic name

Time:08-08

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?

CodePudding user response:

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.

  • Related