6.1.1基本用法和示例

以下是在Octave中使用数据结构的一些示例。

结构体的元素可以是任何值类型。例如,三个表达式

x.a = 1;
x.b = [1, 2; 3, 4];
x.c = "string";

创建一个包含三个元素的结构体。这个.character separate'结构体名称(在上面的例子中x)从字段名称,并向Octave指示此变量是一个结构体。要打印结构体的值,您可以输入其名称,就像输入任何其他变量一样:

x
     ⇒ x =

         scalar structure containing the fields:

           a =  1
           b =

              1   2
              3   4

           c = string

请注意,Octave可以按任何顺序打印元素。

结构体可以像任何其他变量一样进行复制:

y = x
     ⇒ y =

         scalar structure containing the fields:

           a =  1
           b =

              1   2
              3   4

           c = string

从于结构体本身就是值,因此结构体元素也可以引用其他结构体。以下语句添加字段d到结构体x.字段的值d本身是包含单个字段的数据结构a,其值为3。

x.d.a = 3;
x.d
     ⇒ ans =

         scalar structure containing the fields:

           a =  3

x
     ⇒ x =

         scalar structure containing the fields:

           a =  1
           b =

              1   2
              3   4

           c = string
           d =

             scalar structure containing the fields:

               a =  3

请注意,当Octave打印包含其他结构体的结构体的值时,只显示几个级别。例如

a.b.c.d.e = 1;
a
     ⇒ a =

         scalar structure containing the fields:

           b =

             scalar structure containing the fields:

               c =

                 scalar structure containing the fields:

                   d: 1x1 scalar struct

这可以防止大型深度嵌套结构体的长时间且令人困惑的输出。嵌套结构体要打印的级别数可能会受到函数的困扰struct_levels_to_print,以及函数print_struct_array_contents可以用于实现结构体数组的内容的打印。

 
: val = struct_levels_to_print ()
: old_val = struct_levels_to_print (new_val)
: old_val = struct_levels_to_print (new_val, "local")

查询或设置指定要显示的结构体级别数的内部变量。

当从具有的函数内部调用时"local"参数,则该变量会为函数及其调用的任何子程序在本地进行更改。退出函数时将恢复原始变量值。

详见: print_struct_array_contents.

广告
 
: val = print_struct_array_contents ()
: old_val = print_struct_array_contents (new_val)
: old_val = print_struct_array_contents (new_val, "local")

查询或设置指定是否打印structarray内容的内部变量。

如果为true,则打印结构体数组元素的值。此变量不影响元素始终打印的标量结构体。然而,在这两种情况下,打印将仅限于struct_levels_to_print.

当从具有的函数内部调用时"local"参数,则该变量会为函数及其调用的任何子程序在本地进行更改。退出函数时将恢复原始变量值。

详见: struct_levels_to_print.

广告

函数可以返回结构体。例如,以下函数将矩阵的实部和复部分离,并将它们存储在同一结构体变量的两个元素中y.

function y = f (x)
  y.re = real (x);
  y.im = imag (x);
endfunction

当使用复值参数调用时,函数f返回包含原始函数参数的实部和虚部的数据结构。

f (rand (2) + rand (2) * I)
     ⇒ ans =

         scalar structure containing the fields:

           re =

              0.040239  0.242160
              0.238081  0.402523

           im =

              0.26475  0.14828
              0.18436  0.83669

函数返回列表可以包括结构体元素,并且它们可以像任何其他变量一样进行索引。例如

[ x.u, x.s(2:3,2:3), x.v ] = svd ([1, 2; 3, 4]);
x

     ⇒ x =

         scalar structure containing the fields:

           u =

             -0.40455  -0.91451
             -0.91451   0.40455

           s =

              0.00000   0.00000   0.00000
              0.00000   5.46499   0.00000
              0.00000   0.00000   0.36597

           v =

             -0.57605   0.81742
             -0.81742  -0.57605

也可以使用的特殊形式在循环中循环遍历结构体的所有元素for语句(详见在结构体元素上循环).


版权所有 © 2024-2025 Octave中文网

ICP备案/许可证号:黑ICP备2024030411号-2