6.1.1 基本用法和示例

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

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

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

创建了一个包含三个元素的结构体。其中 . 字符将结构体名称(在上面的例子中为 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

由于结构体本身也是值,因此结构体元素也可以引用其他结构体。以下语句向结构体 x 添加了字段 d。字段 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")

查询或设置指定是否打印结构体数组内容的内部变量。

如果为 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-2026 Octave中文网

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