【问题标题】:Display struct fields without the mess显示结构字段而不乱
【发布时间】:2017-06-20 21:48:09
【问题描述】:

我在 Octave 中有一个包含一些大数组的结构。

我想知道这个结构中字段的名称,而不必查看所有这些大数组。

例如,如果我有:

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

了解结构的明显方法如下:

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

在 Matlab 中,这看起来更简洁:

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

如何在不查看所有这些大数组的情况下查看字段/字段名称?

有没有办法在 Octave 中显示简洁的概述(如 Matlab 的)?

谢谢!

【问题讨论】:

    标签: octave


    【解决方案1】:

    您可能想看看Basic Usage & Examples。提到的几个功能听起来像是它们将控制终端中的显示。

    • struct_levels_to_print
    • print_struct_array_contents

    这两个功能听起来像是在做你想做的事。我都尝试了,但无法让第二个工作。第一个函数改变了终端输出,如下所示:

    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
    }
    

    我正在运行旧版本的 Octave。

    octave:8> version
    ans = 3.2.4
    

    如果有机会,我会检查其他函数print_struct_array_contents,看看它是否符合您的要求。 Octave 3.6.2 looks to be the latest version 截至 2012 年 11 月。

    【讨论】:

    • 谢谢,Sim,这正是我想要的。太糟糕了,它不会只递归打印字段的名称,但这是相当不错的。
    • 我在 v3.6.2 上尝试了 print_struct_array_contents 函数,但它没有达到我的预期,所以除非其他人有更好的想法,否则我认为这可能是你最好的选择。
    【解决方案2】:

    使用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
    }
    

    或者您希望它是递归的,请将以下内容添加到您的 .octaverc 文件中(您可能需要根据自己的喜好进行调整)

    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
    

    然后您可以通过以下方式使用它:

    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】:

      在 Octave 中,版本 4.0.0 配置为“x86_64-pc-linux-gnu”。(Ubuntu 16.04) 我是在命令行上这样做的:

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

      输出:(缩短):

      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    
              } 
      

      禁用/恢复旧行为

      print_struct_array_contents(false)
      sampleFactorList
      sampleFactorList =
      
        scalar structure containing the fields:
      
          sampleFactorList =
      
            1x6 struct array containing the fields:
      
              var
              card
              val
      

      我已将此print_struct_array_contents(true) 也放入.octaverc 文件中。

      【讨论】:

        猜你喜欢
        • 2022-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多