help-octave
[Top][All Lists]
Advanced

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

accessing "sub-structs"


From: Thorsten Meyer
Subject: accessing "sub-structs"
Date: Sat, 06 Jun 2009 17:37:54 +0200
User-agent: Mozilla-Thunderbird 2.0.0.19 (X11/20090103)

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



reply via email to

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