Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: struct command in loop
Replies: 5   Last Post: Apr 1, 2013 6:23 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Kian

Posts: 12
Registered: 4/16/13
Re: struct command in loop
Posted: Apr 1, 2013 6:05 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Steven_Lord" <slord@mathworks.com> wrote in message <kjchtq$sia$1@newscl01ah.mathworks.com>...
>
>
> "Kian " <kianian@gmail.com> wrote in message
> news:kjcgrl$ok3$1@newscl01ah.mathworks.com...

> > I have a for loop, where in each loop I want to put a 12 by 4 matrix into
> > a structure.
> > The 12 by 4 matrix is generated in each loop, so then I want to assign
> > each newly generated matrix into a new field of the same structure.
> >
> > So for example I have this matrix called "Mat", and its elements are
> > regenerated in each loop, so in the first loop I can write something like
> >
> > field = 'field1'
> > mystruct = struct (field, Mat)
> >
> > then in the second loop I have to write something to change my "field"
> > script, like in the second loop it should be changed to
> >
> > field = 'field2'
> >
> > so that once struct is run, it assigns the new "Mat" to another field. My
> > problem is how to write field so that is changes from 'field1' to 'field2'
> > and so on in each loop!
> >
> > Sorry, I know this is easy for you guys out there, but I m sort of a
> > novice!

>
> Use dynamic field names.
>
> http://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html
>
> field = 'field1';
> mystruct.(field) = Mat;
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Thank you very much Steve, I got it right.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.