Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
struct command in loop

Subject: struct command in loop

From: Kian

Date: 1 Apr, 2013 17:41:09

Message: 1 of 6

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!

I appreciate any help.

Subject: struct command in loop

From: Steven_Lord

Date: 1 Apr, 2013 17:59:22

Message: 2 of 6



"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

Subject: struct command in loop

From: TideMan

Date: 1 Apr, 2013 20:56:53

Message: 3 of 6

On Tuesday, April 2, 2013 6:41:09 AM UTC+13, Kian wrote:
> 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!
>
>
>
> I appreciate any help.

Two things come to mind:
1. If all the matrices are the same size, why not use a multi-dimensional array instead:
  A(12,4,iz)
2. If you need to use a structure, maybe use a structure array:
    mystruct(iz).field=A;
where iz is the loop index.

Subject: struct command in loop

From: Kian

Date: 1 Apr, 2013 22:05:13

Message: 4 of 6

"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.

Subject: struct command in loop

From: Kian

Date: 1 Apr, 2013 22:21:11

Message: 5 of 6

Thank you, I got it from both of you guys explaining this myStruct.(field)=Mat thing for me. That was exactly what I was looking for.

Thanks all.


TideMan <mulgor@gmail.com> wrote in message <b0abc0d1-a334-4736-b94b-919820258b30@googlegroups.com>...
> On Tuesday, April 2, 2013 6:41:09 AM UTC+13, Kian wrote:
> > 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!
> >
> >
> >
> > I appreciate any help.
>
> Two things come to mind:
> 1. If all the matrices are the same size, why not use a multi-dimensional array instead:
> A(12,4,iz)
> 2. If you need to use a structure, maybe use a structure array:
> mystruct(iz).field=A;
> where iz is the loop index.

Subject: struct command in loop

From: Kian

Date: 1 Apr, 2013 22:23:14

Message: 6 of 6

TideMan <mulgor@gmail.com> wrote in message <b0abc0d1-a334-4736-b94b-919820258b30@googlegroups.com>...
> On Tuesday, April 2, 2013 6:41:09 AM UTC+13, Kian wrote:
> > 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!
> >
> >
> >
> > I appreciate any help.
>
> Two things come to mind:
> 1. If all the matrices are the same size, why not use a multi-dimensional array instead:
> A(12,4,iz)
> 2. If you need to use a structure, maybe use a structure array:
> mystruct(iz).field=A;
> where iz is the loop index.

Thank you Tideman. I got it.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us