Display struct fields without the mess

北城以北 提交于 2019-12-02 23:44:26

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.

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

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.

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