help-octave
[Top][All Lists]
Advanced

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

Re: How to extract all variables from a structure


From: Terry Duell
Subject: Re: How to extract all variables from a structure
Date: Fri, 29 Mar 2013 13:05:59 +1100
User-agent: Opera Mail/12.14 (Linux)

On Thu, 28 Mar 2013 21:34:46 +1100, Andy Buckle <address@hidden> wrote:

On 28 March 2013 09:36, Terry Duell <address@hidden> wrote:


I see that I can use the function "getfield" to extract the structure
"data" from the structure "s".
Is there a neat way of extracting all the variables from "data", (ie. so
that they are simply unstructured variables) without having to separately
reference each?

Cheers,


I don't think it is possible. The best you can do is use fieldnames, and
then go through them using getfield. If it's nested and you can't make
assumptions about how it's nested, then make it recursive.


I'm not getting anywhere useful with this.
I can extract the inner structure, and get the fieldnames, as follows...

octave:2> s
s =

  scalar structure containing the fields:

    data =

      scalar structure containing the fields:

        a =  7040
        b =  237.60
        c =  244.20
        d =  150
        e =  3000
        f =  14.500
        g =  14.500
        h =  14.500


octave:3> data = getfield(s,"data");
octave:4> data
data =

  scalar structure containing the fields:

    a =  7040
    b =  237.60
    c =  244.20
    d =  150
    e =  3000
    f =  14.500
    g =  14.500
    h =  14.500

octave:5> list = fieldnames(data);
octave:6> list
list =
{
  [1,1] = a
  [2,1] = b
  [3,1] = c
  [4,1] = d
  [5,1] = e
  [6,1] = f
  [7,1] = g
  [8,1] = h
}
octave:7> newlist = cell2mat(list)
newlist =

a
b
c
d
e
f
g
h

...but get myself all tangled up when I try to use the fieldnames (list, or newlist) with the function "getfield" to extract all the fields from "data".
Can someone please give me a clue as to how I should tackle that?

Cheers,
--
Regards,
Terry Duell


reply via email to

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