2012-11-22 11 views
11

मेरे पास ऑक्टेव में एक संरचना है जिसमें कुछ बड़े सरणी शामिल हैं।गड़बड़ के बिना प्रदर्शन फ़ील्ड फ़ील्ड

मैं इन सभी बड़े सरणीओं को देखे बिना इस संरचना में फ़ील्ड के नाम जानना चाहता हूं।

उदाहरण के लिए, अगर मेरे पास है:

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 

इन सभी बड़े सरणी को देखे बिना फ़ील्ड/फील्ड नाम कैसे देख सकते हैं?

क्या ऑक्टेव के अंदर एक संक्षिप्त अवलोकन (जैसे मैटलैब) प्रदर्शित करने का कोई तरीका है?

धन्यवाद!

उत्तर

12

आप 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:8> version 
ans = 3.2.4 

अगर मैं मुझे लगता है कि अन्य समारोह, print_struct_array_contents की जांच करेंगे, अगर यह तुम क्या चाहते करता है देखने के लिए एक मौका मिलता है। 11/2012 के रूप में Octave 3.6.2 looks to be the latest version

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

या आप आप इसे पुनरावर्ती होना चाहता हूँ, (आप अपनी पसन्द के अनुसार इसे समायोजित कर सकते हैं)

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 

तब आप अपने .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

ऑक्टेव में, संस्करण 4.0.0 con "x86_64-pc-linux-gnu" के लिए लगाया गया। (उबंटू 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 फ़ाइल में भी डालें।

संबंधित मुद्दे