[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: cycling over structure elements
From: |
Andy Buckle |
Subject: |
Re: cycling over structure elements |
Date: |
Mon, 2 Aug 2010 08:49:49 +0100 |
On Mon, Aug 2, 2010 at 4:33 AM, Mike B. <address@hidden> wrote:
> Hi All,
>
> Assuming I have a strcutre defined:
> s.a = 1;
> s.b = 2;
>
> Is it possible to cycle over strucutre elements and to define new variables
> which have a fixed prefix + the structure element name, for example with the
> above structure the pseudo-code is
>
> {cycle over elements of s }
> { for each element define a variable x_<element name> = s.<element name> }
>
> which results in new variables:
>
> x_a = s.a;
> x_b = s.b;
>
> Thanks,
> Mike.
This works. I don't think this method will scale well, though.
--------------------
>s.a=1;
>s.b=2;
>fieldnames (s)
ans =
{
[1,1] = a
[2,1] = b
}
>fn=fieldnames(s);
>for i=1:length(fn);eval(["x_" char(fn(i)) " = s." char(fn(i)) ";" ]); end;
>x_a
x_a = 1
--------------------
I kind of expect someone who knows more to suggest something better.
Andy