Documentation

struct

创建结构数组

Syntax

s = struct
s = struct(字段,值)
s = struct(field1,value1,...,fieldN,valueN)
s = struct([])
s = struct(obj)

Description

s = struct创建一个没有字段的标量(1-by-1)结构。

example

s = struct(field,价值)使用指定的字段和值创建一个结构数组。这价值输入参数可以是任何数据类型,例如数字,逻辑,字符或单元格数组。

  • If价值isnot然后一个单元格数阵列sis a scalar structure, wheres。(字段)=值

  • If价值is a cell array, thensis a structure array with the same dimensions as价值。每个元素scontains the corresponding element of价值。For example,s = struct('f',{'a','b'})returnss(1).f = 'a's(2).f = 'b'

  • If价值是一个空的单元格数组{}, thens是一个empty (0-by-0) structure.

example

s = struct(field1,value1,...,fieldN,valueN)creates a structure array with multiple fields. Any nonscalar cell arrays in the setValue1,...,Valuen必须具有相同的维度。

  • 如果没有价值inputs is a cell array, or if all价值inputs that are cell arrays are scalars, thens是标量结构。

  • 如果有价值inputs is a nonscalar cell array, thens具有与非尺度单元阵列相同的尺寸。任何价值that is a scalar cell array or an array of any other data type,struct插入的内容价值in the relevant field for all elements ofs

  • If any价值输入是一个空的单元格数组,{}, then outputs是一个empty (0-by-0) structure. To specify an empty field and keep the values of the other fields, use[]作为一个价值输入。

s = struct([])创建一个没有字段的空结构(0 x-0)结构。

s = struct(obj)创建具有字段名称和值的标量结构,与obj。这structfunction does not convertobj,而是创造s作为一个new structure. This structure does not retain the class information, so private, protected, and hidden properties become public fields ins。这structfunction issues a warning when you use this syntax.

Examples

collapse all

使用一个字段创建一个非大规模结构。

field ='f';价值= {“一些文字”;[10, 20, 30]; magic(5)}; s = struct(field,value)
s =3×1 struct array with fields:f

查看每个元素的内容。

S.F
ans ='一些文字'
ans =10 20 30
ans =17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

When you access a field of a nonscalar structure, such asS.F,Matlab®返回逗号分隔的列表。在这种情况下,S.Fis equivalent tos(1).f,s(2).f,s(3).f

创建一个具有多个字段的非标准结构。

field1 ='f1';value1 =零(1,10);field2 ='f2';价值2 = {'a','b'};field3 ='f3';价值3 = {pi, pi.^2}; field4 ='f4';价值4 = {'fourth'};s = struct(field1,value1,field2,value2,field3,value3,field4,value4)
s =带有字段的1×2结构阵列:f1 f2 f3 f4

这细胞arrays for价值2价值3是1 x-2,所以sis also 1-by-2. Because价值1is a numeric array and not a cell array, boths(1).f1s(2).f1具有相同的内容。同样,因为用于价值4有一个元素s(1).f4s(2).f4具有相同的内容。

s(1)
ans =struct with fields:f1: [0 0 0 0 0 0 0 0 0 0] f2: 'a' f3: 3.1416 f4: 'fourth'
s(2)
ans =struct with fields:f1: [0 0 0 0 0 0 0 0 0 0] f2: 'b' f3: 9.8696 f4: 'fourth'

用空字段创建一个结构。利用[]指定空字段的值。

s = struct('f1','a','f2',[])
s =struct with fields:f1: 'a' f2: []

使用包含单元格数组的字段创建一个结构。

field ='Mycell';价值= {{'a','b','c'}}; s = struct(field,value)
s =struct with fields:mycell: {'a' 'b' 'c'}

创建一个带有几个字段的空结构。

s = struct('a',{},'b',{},'c',{})
S = 0×0带有字段的空结构数组:A B C

为空结构中的字段分配值。

s(1).a ='a'
s =struct with fields:a: 'a' b: [] c: []

Create a nested structure.ais a structure with a field which contains another structure.

a.b = struct('c',{},'d',{})
a =struct with fields:b: [0×0 struct]

View the names of the fields ofa.b

字面名称(a.b)
ans =2×1 cell array'c''d'

Input Arguments

collapse all

字段名称,指定为字符向量。有效的字段名称以字母开头,可以包含字母,数字和下划线。字段名称的最大长度是值namelengthmaxfunction returns.

Values within a structure field, specified as a cell array or as a scalar, vector, or multidimensional array of any other data type.

如果没有价值inputs is a cell array, or if all价值inputs that are cell arrays are scalars, then outputs是标量结构。否则,价值非刻级单元格数组的输入必须具有相同的尺寸,并且输出salso has those dimensions. For any价值that is a scalar cell array or an array of any other data type,struct插入的内容价值in the relevant field for all elements ofs

If any价值输入是一个空的单元格数组,{}, then outputs是一个empty structure array. To specify an empty field and keep the values of the other fields, use[]作为一个价值输入。

Data Types:single|double|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64|logical|char|struct|桌子|细胞|function_handle|分类|约会时间|duration|calendarDuration
Complex Number Support:Yes

Object.struct复制特性objto the fields of a new scalar structure.objcannot be an object of a fundamental data type, such asdouble或者char。看基本MATLAB课程for the list of fundamental data types.

Extended Capabilities

在R2006a之前引入

这个话题有帮助吗?