help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cycling over structure elements


From: Jaroslav Hajek
Subject: Re: cycling over structure elements
Date: Mon, 2 Aug 2010 09:51:33 +0200

On Mon, Aug 2, 2010 at 9:49 AM, Andy Buckle <address@hidden> wrote:
> 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.
>


See

http://octave.sourceforge.net/general/function/unpackfields.html



-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]