2012-11-22 3 views
11

큰 배열이 들어있는 Octave 구조체가 있습니다.엉망이없는 구조체 필드 표시

이 구조체의 모든 필드를 살펴 보지 않고도이 구조체의 필드 이름을 알고 싶습니다. 예를 들어

에게, 내가있는 경우 :

매트랩
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 

이러한 큰 배열을 모두 보지 않고 어떻게 필드/필드 이름을 볼 수 있습니까?

옥타브 안에 간결한 개요 (예 : Matlab)를 표시 할 수있는 방법이 있습니까?

감사합니다.

답변

12

Basic Usage & Examples을 살펴볼 수 있습니다. 터미널에 표시하는 것을 제어하는 ​​것처럼 들리는 몇 가지 기능이 있습니다. 그들은 당신이 원하는 것을 할 수있는 것처럼

  • print_struct_array_contents
  • 이 두 함수 struct_levels_to_print

    • 소리. 나는 둘 다 시도해 보았고 두 번째 작품을 얻을 수 없었다. 첫 번째 기능은 다음과 같이 터미널 출력을 변경했습니다.

      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, 2012 년 11 월 11 일 그런 다음 수

      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 
      

      을 (당신이 당신의 환경 설정을 조정할 수 있습니다)

    +0

    고마워요, 심,이게 내가 찾고 있던 것 같았 어. 너무 나쁘다는 것은 필드의 이름 만 재귀 적으로 인쇄하지는 않지만 이것은 꽤 괜찮습니다. – Richard

    +0

    v3.6.2에서 print_struct_array_contents 함수를 시도했지만 예상했던대로 작동하지 않았습니다. 다른 누군가가 내 생각에 더 좋은 아이디어를 갖고 있지 않으면 최선의 선택이라고 생각합니다. – 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 파일에 다음을 추가 다음과 같이 사용하십시오.

    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, 버전 4.0.0 con "x86_64-pc-linux-gnu"라고 생각했습니다. (Ubuntu 16.

    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 
    

    다시 얻으려면 04) 나는 명령 행에서 이런 짓을 이 print_struct_array_contents(true).octaverc 파일에 넣으십시오.