Display struct fields without the mess

喜欢而已 提交于 2019-12-03 10:20:04

问题


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!


回答1:


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.




回答2:


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



回答3:


in Octave, version 4.0.0 configured for "x86_64-pc-linux-gnu".(Ubuntu 16.04) I did this on the command line:

print_struct_array_contents(true)
sampleFactorList    % example, my nested structure array

Output: (shortened):

sampleFactorList =

  scalar structure containing the fields:

    sampleFactorList =

      1x6 struct array containing the fields:

        var =
        {
          [1,1] =  1
          [1,2] =   
             2   1   3  
        }

        card =
        {
          [1,1] =  3
          [1,2] =
             3   3   3    
        } 

To disable/get back to the old behaviour

print_struct_array_contents(false)
sampleFactorList
sampleFactorList =

  scalar structure containing the fields:

    sampleFactorList =

      1x6 struct array containing the fields:

        var
        card
        val

I've put this print_struct_array_contents(true) also into the .octaverc file.



来源:https://stackoverflow.com/questions/13512743/display-struct-fields-without-the-mess

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!