help-octave
[Top][All Lists]
Advanced

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

Re: accessing "sub-structs"


From: Jaroslav Hajek
Subject: Re: accessing "sub-structs"
Date: Sat, 6 Jun 2009 18:07:39 +0200

On Sat, Jun 6, 2009 at 5:37 PM, Thorsten Meyer<address@hidden> wrote:
> Hi,
>
> given a structure
>  s = struct("a", 1, "b", 2, "c", 3)
>
> I would like to access a substructure of s containing only the fields "b" and 
> "c".
>
> Of course, this can be done by
>  substruct = struct("b", s.("b"), "c", s.("c"));
> or in an assignment:
>  [s.b, s.c] = deal(10, 20);
>
> However, how can I do it, if I do not know beforehand, which fields I want to
> access? E.g., I would like to create a function, where a structure and
> cellstring containing a list of fieldnames is given as input arguments, and 
> the
> corresponding substructure is returned.
> Of course, I can generate a command string like the above, with sprintf and 
> eval it:
>  substruct = eval(['struct (', ...
>      sprintf('"%s", s.("%s"),', [fields; fields]{:})(1:end-1), ...
>      ")"]);
> But is there a more elegant way to do it?
>
> regards
>
> Thorsten
>

Some time ago there was a proposal to allow cs-lists being specified
as dynamic field indices, i.e.

ss = s.(fields{:}); # fields is a cell array
ss = s.("a","b");

Not a bad idea, IMHO. But I would prefer to see examples of real-life
usage first.

-- 
RNDr. Jaroslav Hajek
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]