2012-11-22 10 views
11

大きな配列を含むOctaveの構造体があります。混乱のない構造体フィールドを表示

この構造体のフィールドの名前を知りたいのですが、これらすべての大きな配列を見る必要はありません。例えば

私が持っている場合は、:MATLABで

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 

を、これはより簡潔として表示されます:

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

を構造でチラッを取るために明白な方法は次のとおりです。

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

これらの大きな配列をすべて見ずにフィールド/フィールド名を確認するにはどうすればよいですか?

Octaveの中に簡潔な概要(Matlabのようなもの)を表示する方法はありますか?

ありがとうございます!

答えて

12

Basic Usage & Examplesをご覧ください。いくつかの機能がありますが、ターミナルでの表示を制御するように聞こえます。

  • struct_levels_to_print
  • print_struct_array_contents

彼らはあなたがやりたいようにしているこれらの2つの機能は、音。私は両方を試みて、2番目のものを動かすことができませんでした。 1番目の関数は端末出力を次のように変更しました。

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、そのほかの機能をチェックします。オクターブ3.6.2 looks to be the latest version 11/2012現在。

+0

ありがとう、シムこれは私が探していたほとんどだった。あまりにもフィールドの名前だけを再帰的に表示しないのは悪いですが、これはまともです。 – Richard

+0

私はprint_struct_array_contents関数をv3.6.2で試しましたが、他の人があなたの最善の選択肢かもしれないと思うよりよいアイデアがない限り、私が期待したことはしませんでした。 – slm

3

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

Octave、version 4.0.0 con "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ファイルに入れてください。

関連する問題