Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

Structure in MATLAB: Create, Access and Nest Structs and Struct Arrays

How to create, access, add to and nest a structure in MATLAB, plus struct arrays and the struct() cell-array gotcha, from MathWorks documentation.

Written and maintained by CASRAI Editorial Board

Last updated

A structure (struct) in MATLAB groups related values under named fields, so a participant record, an instrument setting block or a set of analysis parameters travels as one variable. This guide shows how to create a structure, read and add fields, nest structures, build structure arrays and generate field names from variables, then covers the one struct call that surprises people and how to decide between a struct, a cell array and a table. Every function and rule below is taken from the MathWorks documentation for the current MATLAB release, read 2026-11-07.

What structures are used for in research

A structure stores data of different types and sizes in one variable by giving each piece a name. Researchers use them for metadata that should stay attached to data (subject ID, sampling rate, units), for parameter sets passed between functions, and for records where each observation has the same named fields. The MathWorks documentation notes that you can create a structure array of any size as long as every structure in the array has the same fields.

Access: where structures are available

Structures are a core MATLAB data type, so no add-on toolbox is needed. MATLAB is distributed by MathWorks; get it, and its documentation, only from mathworks.com. Licensing options are set by MathWorks and your institution, and are not covered here. A free alternative is covered in the table near the end.

Step by step: create, access, add and nest a structure

1. Create a structure (structure in MATLAB)

Three forms of struct are documented:

  1. s = struct creates a 1-by-1 structure with no fields.
  2. s = struct(field,value) and s = struct(field1,value1,...,fieldN,valueN) create a structure with the named fields and values, for example s = struct('subject','S01','rate',250).
  3. s = struct([]) creates an empty 0-by-0 structure.

Field names can contain ASCII letters (A-Z, a-z), digits (0-9) and underscores, and must begin with a letter.

2. Access and add fields with dot notation

Read or assign a field with structName.fieldName. Assigning to a field that does not yet exist adds it, which is how you add to a struct after creating it:

s.subject = 'S01';
s.rate = 250;
s.rate

The related functions that MathWorks lists on its structures page for working with fields are fieldnames, getfield, setfield, isfield, isstruct, rmfield and orderfields; look each up in the documentation before use.

3. Nested structures

A field can itself hold a structure. Chain dot notation to reach the inner value, for example s.device.name. MathWorks documents this under “Access Data in Nested Structures”.

4. Structure arrays (structures in MATLAB)

Index a structure variable to build an array of structures. The documentation example builds a patient record this way:

patient.name = 'John Doe';
patient.billing = 127;
patient(2).name = 'Ann Lane';
patient(2).billing = 28.50;

If you then assign only patient(3).name, the unspecified fields of element 3 contain empty arrays. You can index into part of a field only for a single structure element, as in patient(2).test(:,[1 2]). For very large structure arrays, MathWorks advises preallocating rather than growing the array incrementally, and notes that the elements of a structure array need not be contiguous in memory but each field’s data must be.

5. Generate field names from variables

Dynamic field names use the syntax structName.(dynamicExpression). The documented example uses a date string as the field name:

currentDate = datestr(now,'mmmdd');
myStruct.(currentDate) = [1,2,3];

6. Create a structure array from cell arrays in one call

If a value passed to struct is a nonscalar cell array, the result is a structure array with the same dimensions as that cell array. A scalar cell or a non-cell value is copied into every element. The documented example:

s = struct('x',{'a','b'},'y','c')

This returns a 1-by-2 structure array where s(1).x is 'a', s(2).x is 'b', and both s(1).y and s(2).y are 'c'. If you pass several nonscalar cell arrays, they must be the same size.

Output and how to read it

A 1-by-1 structure displays its field names and values. A structure array displays its size and field names. When you access a field of a nonscalar structure, such as s.f, MATLAB returns a comma-separated list, one value per element, not a single array. To apply a function across fields or elements, MathWorks lists arrayfun and structfun among the related functions.

Struct, cell array or table

MathWorks has a documentation topic, “Ways to Organize Data in Structure Arrays”, on choosing between an array of structures and a structure of arrays. The structures page lists struct2table and cell2struct for converting between a structure and a table or cell array. As a pointer rather than a rule: a structure suits a record with named fields of mixed types; a table suits rectangular, column-oriented data such as one variable per column. See the CASRAI guide to MATLAB tables for that format, and matrices in MATLAB for numeric arrays.

Troubleshooting

Symptom Documented cause
A field name is rejected Field names may contain only ASCII letters, digits and underscores, and must begin with a letter.
s.f returns several answers instead of one Accessing a field of a nonscalar structure returns a comma-separated list.
struct errors with several cell inputs Multiple nonscalar cell inputs must be the same size.
struct('f',{}) returns an empty structure, not a field holding an empty array A {} value gives an empty 0-by-0 structure; use [] to keep an empty field.
A new array element has empty fields Unspecified fields of a new element contain empty arrays.
Indexing part of a field fails on an array You can index into part of a field only for a single structure element.

Related reading: loops in MATLAB for iterating over structure array elements, saving workspace variables to MAT files to store a structure, and anonymous functions for use with arrayfun.

Free alternatives

Tool What its documentation says about structures
GNU Octave Its manual’s Structures chapter states that the current implementation uses an associative array with indices limited to strings.

FAQ

What is a structure in MATLAB?

A data type that groups values under named fields, accessed with structName.fieldName. Create one with struct.

How do I add to a struct in MATLAB?

Assign to a new field name with dot notation, for example s.units = 'mV', or to a new index to extend a structure array, for example patient(3).name = 'Sam'. Every element in a structure array shares the same fields.

How do I create a nested structure in MATLAB?

Assign a structure to a field, or chain dot notation such as s.device.name = 'amp'.

How do I convert a structure to a table or array?

MathWorks lists struct2table and cell2struct among the structure conversion functions; read their reference pages for syntax and the shapes of input they accept.

Should I use a struct or a table?

See the section above and the MATLAB table guide; MathWorks’ “Ways to Organize Data in Structure Arrays” topic covers the structure-array side.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Structure in MATLAB: Create, Access and Nest Structs and Struct Arrays

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →