Structures in MATLAB


Revision as of 19:11, 15 April 2013 by Gabe Spradlin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Structures in MATLAB
Green carrot left.gif
All MATLAB Articles MATLAB Arrays
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.

1 Introduction to Structures

MATLAB provides a means for structure data elements. Structures are created and accessed in a manner familiar for those accustomed to programming in C.

MATLAB has multiple ways of defining and accessing structure fields. See Declaring Structures for more details.

Follow this link for MATLAB structure help.

Note: Structure field names must begin with a letter, and are case-sensitive. The rest of the name may contain letters, numerals, and underscore characters. Use the namelengthmax function to determine the maximum length of a field name.

2 Declaring Structures

Structures can be declared using the struct command.

   >> a = struct('b', 0, 'c', 'test')
   a = 
     b: 0
     c: 'test'

In MATLAB, variables do not require explicit declaration before their use. As a result structures can be declared with the '.' operator.

   >> b.c = 'test'
   b =
     b: 0
     c: 'test'

Structures can be declared as needed and so can the fields.

3 Arrays of Structures

Structures can also be arrays. Below is an example

   >> a = struct('b', 0, 'c', 'test');            % Create structure
   >> a(2).b = 1;                                 % Turn it into an array by creating another element
   >> a(2).c = 'testing'
        a =
          1x2 struct array with fields:
   >> a(1)                                        % Initial structure
        ans = 
          b: 0
          c: 'test'
   >> a(2)                                        % The second element
        ans = 
          b: 1
          c: 'testing'

4 Accessing Fields

When the field name is known the field value can be accessed directly.

   >> a.c
       ans =
       ans =

In some cases you may need to access the field dynamically which can be done as follows.

   >> str = 'c';
   >> a(1).(str)
       ans =
   >> a(1).c
       ans =

5 Accessing Array Elements

Any given element in a structure array can be accessed through an array index like this

   >> a(1).c
       ans =

To access all elements in a structure array use the syntax {structure}.{field}. In order to get all values in a vector or array use the brackets ([]) as seen below.

   >> [a.('c')]
       ans =
   >> [a.('b')]
       ans =
          0     1

6 Properties and Methods

6.1 Properties

  • isstruct: Return true for structures.
  • isfield: Return true if the field is in a structure array.

6.2 Methods

  • fieldnames: Get field names of the structure array.
  • orderfields: Order fields of structure array.
  • getfield: Get value of a field of the structure array.
  • setfield: Set value of a field of the structure array.
  • rmfield: Remove a structure field.
  • deal: Deal inputs to outputs.
  • struct2cell: Convert a structure array into a cell array.
  • fieldnames: Get structure field names.