2012-11-22 7 views
11

Ho una struttura in Octave che contiene alcuni grandi array.Mostra i campi della struct senza il pasticcio

Mi piacerebbe sapere i nomi dei campi in questa struct, senza dover guardare tutti questi grandi array.

Ad esempio, se ho:

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

Il modo ovvio per date un'occhiata la struttura è la seguente:

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, questo viene visualizzato nel più breve:

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

Come posso vedere i nomi dei campi/campo senza vedere tutti questi grandi array?

Esiste un modo per visualizzare una breve panoramica (come Matlab di) all'interno di Octave?

Grazie!

risposta

12

Si potrebbe voler dare un'occhiata a Basic Usage & Examples. Ci sono diverse funzioni menzionate che suonano come se controllassero la visualizzazione nel terminale.

  • struct_levels_to_print
  • print_struct_array_contents

Queste due funzioni suonano come se fossero fare quello che vuoi. Ho provato entrambi e non ho potuto far funzionare il secondo. La prima funzione ha modificato l'uscita del terminale in questo modo:

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 
} 

Sto eseguendo una versione precedente di Octave.

octave:8> version 
ans = 3.2.4 

Se ho la possibilità vado a controllare che altra funzione, print_struct_array_contents, per vedere se si fa ciò che si vuole. Octave 3.6.2 looks to be the latest version a partire dall'11/2012.

+0

Grazie, Sim, questo era praticamente quello che stavo cercando. Peccato che non stampi in modo ricorsivo solo i nomi dei campi, ma questo è abbastanza decente. – Richard

+0

Ho provato la funzione print_struct_array_contents su v3.6.2 e non ha fatto ciò che mi aspettavo, a meno che qualcun altro non abbia un'idea migliore penso che potrebbe essere la soluzione migliore. – slm

3

Usa 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 
} 

O si vuole che sia ricorsivo, aggiungere il seguente al file .octaverc (si consiglia di adattarlo alle proprie preferenze)

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 

È quindi possibile utilizzarlo nel modo seguente:

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

in Octave, versione 4.0.0 con pensato per "x86_64-pc-linux-gnu". (Ubuntu 16.04) ho fatto questo sulla riga di comando:

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

uscita: (abbreviato):

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  
     } 

Per disabilitare/tornare al vecchio comportamento

print_struct_array_contents(false) 
sampleFactorList 
sampleFactorList = 

    scalar structure containing the fields: 

    sampleFactorList = 

     1x6 struct array containing the fields: 

     var 
     card 
     val 

ho inserisci questo print_struct_array_contents(true) anche nel file .octaverc.