Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Structure array

A *structure array* is a data type that groups related data
using data containers called *fields*. Each field can contain any
type of data. Access data in a field using dot notation of the form
`structName.fieldName`

.

When you have data to put into a new structure, create the structure using dot notation to name its fields one at a time:

s.a = 1; s.b = {'A','B','C'}

`s = `*struct with fields:*
a: 1
b: {'A' 'B' 'C'}

You also can create a structure array using the `struct`

function,
described below. You can specify many fields simultaneously, or create a nonscalar
structure array.

`s = struct`

`s = struct(field,value)`

`s = struct(field1,value1,...,fieldN,valueN)`

`s = struct([])`

`s = struct(obj)`

`s = struct`

creates a scalar (1-by-1) structure with
no fields.

`s = struct(`

creates a structure array with the specified field and values. The
`field`

,`value`

)`value`

input argument can be any data type, such as a
numeric, logical, character, or cell array.

If

`value`

is*not*a cell array, or if`value`

is a scalar cell array, then`s`

is a scalar structure. For instance,`s = struct('a',[1 2 3])`

creates a 1-by-1 structure, where`s.a = [1 2 3]`

.If

`value`

is a nonscalar cell array, then`s`

is a structure array with the same dimensions as`value`

. Each element of`s`

contains the corresponding element of`value`

. For example,`s = struct('x',{'a','b'},'y','c')`

returns`s(1).x = 'a'`

,`s(2).x = 'b'`

,`s(1).y = 'c'`

, and`s(2).y = 'c'`

.If

`value`

is an empty cell array`{}`

, then`s`

is an empty (0-by-0) structure.

`s = struct(field1,value1,...,fieldN,valueN)`

creates
multiple fields. Any nonscalar cell arrays in the set
`value1,...,valueN`

must have the same
dimensions.

If none of the

`value`

inputs are cell arrays, or if all`value`

inputs that are cell arrays are scalars, then`s`

is a scalar structure.If any of the

`value`

inputs is a nonscalar cell array, then`s`

has the same dimensions as the nonscalar cell array. For any`value`

that is a scalar cell array or an array of any other data type,`struct`

inserts the contents of`value`

in the relevant field for all elements of`s`

.If any

`value`

input is an empty cell array,`{}`

, then output`s`

is an empty (0-by-0) structure. To specify an empty field and keep the values of the other fields, use`[]`

as a`value`

input instead.

`s = struct([])`

creates an empty (0-by-0) structure
with no fields.

`s = struct(`

creates a scalar
structure with field names and values that correspond to properties of
`obj`

)`obj`

. The `struct`

function does not
convert `obj`

, but rather creates `s`

as a
new structure. This structure does not retain the class information, so
private, protected, and hidden properties become public fields in
`s`

. The `struct`

function issues a
warning when you use this syntax.

`cell2struct`

| `fieldnames`

| `isfield`

| `isstruct`

| `orderfields`

| `rmfield`

| `struct2cell`

| `struct2table`

| `substruct`

| `table`

| `table2struct`

Was this topic helpful?