Thread Subject: structure array in a for

Subject: structure array in a for

From: enric

Date: 12 Jun, 2009 16:55:55

Message: 1 of 4

Hi there!

In a function we are using the following:

for i=1:100

Struct(i) = func2(var1);

end

where func2 is defined in the following way:
function [outp] = func2(input);

where outp is a structure, i.e. for i=1 outp.field=20, outp.field2=40,
etc.....
                                           for i=2 outp.field=30,
outp.field2=50
we would like to store in each step of the loop the variable outp as
an structure cell inside the variable Struct.

Hence, Struct(1).field=20,Struct(1).field2=40
           Struct(2).field=30,Struct(2).field2=50

Do you have any hint? How do we need to define the variables?

The only solution found has been passing the i and the Struct variable
in the function itself as an input and output,
i.e. Struct = func2(var1,Struct,i); and function [output]=func2
(input,Struct,i); but is not very elegant.

Any help on this matter would be very much appreciated!

Thanks a lot,

Enric and Pep

Subject: structure array in a for

From: John

Date: 12 Jun, 2009 18:22:01

Message: 2 of 4

I tried this with the call to func2(var1) inside the for loop and it works fine.
What are you trying to do inside func2. I tried a simple struct assignment like this -

for i=1:100
Struct(i) = func2(i);
end

where I defined func2 as -

function outp = func2(value)
outp.field1=value *10;
outp.field2 = value +10;

Each scalar struct passed back by func2 is assigned to the struct array inside the loop. Depending on your need, there might be other better ways of doing what you want , but the above method is correct as well.

Subject: structure array in a for

From: enric

Date: 15 Jun, 2009 07:34:12

Message: 3 of 4

John thanks a lot for your reply.

However, this is not working for us since you are passing the i
parameter of the for into the function as a value. This is precisely
what we wanted to avoid.

Notice that our func2 is independent of the looping parameters and it
depends on other values.

Kind regards,

Enric and Pep


On 12 Juny, 20:22, "John" <sj...@cnbc.cmu.edu> wrote:
> I tried this with the call to func2(var1) inside the for loop and it works fine.
> What are you trying to do inside func2. I tried a simple struct assignment like this -
>
> for i=1:100
> Struct(i) = func2(i);
> end
>
> where I defined func2 as -
>
> function outp = func2(value)
> outp.field1=value *10;
> outp.field2 = value +10;
>
> Each scalar struct passed back by func2 is assigned to the struct array inside the loop. Depending on your need, there might be other better ways of doing what you want , but the above method is correct as well.

Subject: structure array in a for

From: John

Date: 15 Jun, 2009 11:19:02

Message: 4 of 4

Enric, I passed the loop counter into func2 only as an example. It does not matter how you assign values to outp struct variable inside func2. You can call func2 with any input parameters.

enric <e.girones@gmail.com> wrote in message <4399128a-090e-44de-9cc7-f3bea7910f6d@a7g2000yqk.googlegroups.com>...
> John thanks a lot for your reply.
>
> However, this is not working for us since you are passing the i
> parameter of the for into the function as a value. This is precisely
> what we wanted to avoid.
>
> Notice that our func2 is independent of the looping parameters and it
> depends on other values.
>
> Kind regards,
>
> Enric and Pep
>
>
> On 12 Juny, 20:22, "John" <sj...@cnbc.cmu.edu> wrote:
> > I tried this with the call to func2(var1) inside the for loop and it works fine.
> > What are you trying to do inside func2. I tried a simple struct assignment like this -
> >
> > for i=1:100
> > Struct(i) = func2(i);
> > end
> >
> > where I defined func2 as -
> >
> > function outp = func2(value)
> > outp.field1=value *10;
> > outp.field2 = value +10;
> >
> > Each scalar struct passed back by func2 is assigned to the struct array inside the loop. Depending on your need, there might be other better ways of doing what you want , but the above method is correct as well.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
for Sprinceana 22 Jun, 2009 05:13:05
array Sprinceana 22 Jun, 2009 05:13:05
for loop Sprinceana 22 Jun, 2009 05:13:05
rssFeed for this Thread

Contact us at files@mathworks.com