Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display struct fields without the mess

Tags:

octave

I have a struct in Octave that contains some big arrays.

I'd like to know the names of the fields in this struct without having to look at all these big arrays.

For instance, if I have:

x.a=1;
x.b=rand(3);
x.c=1;

The obvious way to take a gander at the structure is as follows:

octave:12> x
x =

  scalar structure containing the fields:

    a =  1
    b =

       0.7195967   0.9026158   0.8946427   
       0.4647287   0.9561791   0.5932929   
       0.3013618   0.2243270   0.5308220   

    c =  1

In Matlab, this would appear as the more succinct:

 >> x
 x = 
    a: 1
    b: [3x3 double]
    c: 1

How can I see the fields/field names without seeing all these big arrays?

Is there a way to display a succinct overview (like Matlab's) inside Octave?

Thanks!

like image 701
Richard Avatar asked Nov 22 '12 12:11

Richard


2 Answers

You might want to take a look at Basic Usage & Examples. There's several functions mentioned that sound like they'll control the displaying in the terminal.

  • struct_levels_to_print
  • print_struct_array_contents

These two functions sound like they're do what you want. I tried both and couldn't get the 2nd one to work. The 1st function changed the terminal output like so:

octave:1> x.a=1;
octave:2> x.b=rand(3);
octave:3> x.c=1;
octave:4> struct_levels_to_print
ans =  2
octave:5> x
x =
{
  a =  1
  b =

     0.153420   0.587895   0.290646
     0.050167   0.381663   0.330054
     0.026161   0.036876   0.818034

  c =  1
}

octave:6> struct_levels_to_print(0)
octave:7> x
x =
{
  1x1 struct array containing the fields:

    a: 1x1 scalar
    b: 3x3 matrix
    c: 1x1 scalar
}

I'm running a older version of Octave.

octave:8> version
ans = 3.2.4

If I get a chance I'll check that other function, print_struct_array_contents, to see if it does what you want. Octave 3.6.2 looks to be the latest version as of 11/2012.

like image 64
slm Avatar answered Oct 30 '22 01:10

slm


Use fieldnames ()

octave:33> x.a = 1;
octave:34> x.b = rand(3);
octave:35> x.c = 1;
octave:36> fieldnames (x)
ans = 
{
  [1,1] = a
  [2,1] = b
  [3,1] = c
}

Or you you want it to be recursive, add the following to your .octaverc file (you may want to adjust it to your preferences)

function displayfields (x, indent = "")
  if (isempty (indent))
    printf ("%s: ", inputname (1))
  endif
  if (isstruct (x))
    printf ("structure containing the fields:\n");
    indent = [indent "  "];
    nn = fieldnames (x);
    for ii = 1:numel(nn)
      if (isstruct (x.(nn{ii})))
        printf ("%s %s: ", indent, nn{ii});
        displayfields (x.(nn{ii}), indent)
      else
        printf ("%s %s\n", indent, nn{ii})
      endif
    endfor
  else
    display ("not a structure");
  endif
endfunction

You can then use it in the following way:

octave> x.a=1;
octave> x.b=rand(3);
octave> x.c.stuff = {2, 3, 45};
octave> x.c.stuff2 = {"some", "other"};
octave> x.d=1;
octave> displayfields (x)
x: structure containing the fields:
   a
   b
   c: structure containing the fields:
     stuff
     stuff2
   d
like image 28
carandraug Avatar answered Oct 30 '22 03:10

carandraug