Using Structures

About Structures in the Embedded MATLAB Subset

The Embedded MATLAB subset supports a structure data type that is based on the MATLAB structure (see Structures in the MATLAB Programming Fundamentals documentation). By imposing some restrictions, Embedded MATLAB compiles MATLAB structures to generate efficient C code with Real-Time Workshop software. In Embedded MATLAB code, you can perform a subset of the operations available for MATLAB structures, as follows:

Elements of Structures in the Embedded MATLAB Subset

The elements of structures are called fields. Like structures in MATLAB, the fields of a structure in the Embedded MATLAB subset can contain data of any type and size, including:

Types of Structures in the Embedded MATLAB Subset

You can define the following types of structures in Embedded MATLAB:

TypeHow to DefineDetails

Input

Depends on how you use Embedded MATLAB:

  • Define structures in Embedded MATLAB Function blocks based on Simulink bus objects (requires Simulink software)

  • Define primary function inputs based on structure definitions in the MATLAB workspace (to generate C-MEX code using Embedded MATLAB MEX, as described in Working with Embedded MATLAB MEX)

See:

Output

Define structure variable in Embedded MATLAB function

See Defining Outputs as Structures.

Local

Define local structure variable in Embedded MATLAB function

See Defining Local Structure Variables.

Persistent

Declare structure variable to be persistent in Embedded MATLAB function

See Making Structures Persistent.

Defining Local Structure Variables

You can define local structures as variables inside Embedded MATLAB functions. Local structures are temporary by default, but you can make them persistent (see Making Structures Persistent).

You can define structures explicitly as scalars or arrays, as described in these topics:

Defining Scalar Structures

There are several ways to create scalar structures in Embedded MATLAB functions:

Defining Scalar Structures by Extension.   You can create scalar structures by extension by adding fields to a variable using dot notation. For example, the following code creates a structure to represent a point p with coordinates x, y, and z:

...
p.x = 1;
p.y = 3;
p.z = 1;
...

You can also nest scalar structures in direct assignment statements by appending more than one field to a variable using dot notation. For example, the following code adds a color field to structure p:

...
p.color.red = .2;
p.color.green = .4;
p.color.blue = .7;
...

See Indexing Substructures and Fields.

Defining Scalar Structures Using the MATLAB struct Function.   You can create scalar structures in Embedded MATLAB functions using the MATLAB struct function (see Structures in the MATLAB Programming Fundamentals documentation). When using struct in Embedded MATLAB functions, the field arguments must be scalar values. You cannot create structures of cell arrays, but you can define arrays of structures, as described in Defining Arrays of Structures.

Defining Scalar Structures by Assignment.   You can define scalar structures by assigning them to preexisting structures. In the following example, p is defined as a structure that has the same properties as the predefined structure S:

...
S = struct('a',  0, 'b',  1, 'c',  2);
p = S;
...

Defining Arrays of Structures

When you create an array of structures in an Embedded MATLAB function, you must be sure that each structure field in the array has the same size, type, and complexity (see Limitations with Structures). There are several ways to create arrays of structures:

Defining an Array of Structures from a Scalar Structure.   You can create an array of structures from a scalar structure by using the MATLAB repmat function, which replicates and tiles an existing scalar structure. Follow these steps:

  1. Create a scalar structure, as described in Defining Scalar Structures.

  2. Call repmat, passing the scalar structure and the dimensions of the array.

  3. Assign values to each structure using standard array indexing and structure dot notation.

For example, the following code from an Embedded MATLAB function creates X, a 1-by-3 array of scalar structures. Each element of the array is defined by the structure s, which has two fields, a and b:

...
s.a = 0;
s.b = 0;
X = repmat(s,1,3);
X(1).a = 1;
X(2).a = 2;
X(3).a = 3;
X(1).b = 4;
X(2).b = 5;
X(3).b = 6;
...

Defining an Array of Structures Using Concatenation.   To create a small array of structures, you can use the concatenation operator, square brackets ( [ ] ), to join one or more structures into an array (see Concatenating Matrices in the MATLAB Programming Fundamentals documentation). In Embedded MATLAB functions, all the structures that you concatenate must have the same size, class, and complexity.

For example, the following code uses concatenation and a subfunction to create the elements of a 1-by-3 structure array:

...
W = [ sab(1,2) sab(2,3) sab(4,5) ];

function s = sab(a,b)
  s.a = a;
  s.b = b;
...

Defining Outputs as Structures

You define primary function outputs as structures the same way you define local structures (see Defining Local Structure Variables). For example, the following code defines output y as a scalar structure by extension:

function y = fcn(u)
y.a = 1;
y.b = 2;
...

The next example defines output y as a structure with the same fields and values as in the previous example, but this time using the MATLAB struct function:

function y = fcn(u)
y = struct('a',1,'b',2);
...

You can also define outputs as structures by assigning them to a preexisting structure, as in this example:

function y = fcn(u)
x = struct('a',1,'b',2);
y = x;
...

See Structures in the MATLAB Programming Fundamentals documentation.

Making Structures Persistent

To make structures persist, you declare them to be persistent variables and initialize them with the isempty statement, as described in Declaring Persistent Variables.

For example, the following Embedded MATLAB function declares structure X to be persistent and initializes its fields a and b:

function f(u)
persistent X

if isempty(X)
   X.a = 1;
   X.b = 2;
end

Indexing Substructures and Fields

As in MATLAB, you index substructures and fields of structures by using dot notation. Unlike MATLAB, you must reference field values individually in Embedded MATLAB functions (see Limitations with Structures).

For example, the following code excerpt from an Embedded MATLAB function uses dot notation to index fields and substructures:

...
substruct1.a1 = 15.2;
substruct1.a2 = int8([1 2;3 4]);

mystruct = struct('ele1',20.5,'ele2',single(100),
                  'ele3',substruct1);

substruct2 = mystruct;
substruct2.ele3.a2 = 2*(substruct1.a2);
...

The following table shows how Embedded MATLAB resolves symbols in dot notation for indexing elements of the structures in this example:

Dot NotationSymbol Resolution
substruct1.a1Field a1 of local structure substruct1
substruct2.ele3.a1Value of field a1 of field ele3, a substructure of local structure substruct2
substruct2.ele3.a2(1,1)Value in row 1, column 1 of field a2 of field ele3, a substructure of local structure substruct2

Assigning Values to Structures and Fields

Use these guidelines when assigning values to a structure, substructure, or field in Embedded MATLAB functions:

OperationConditions
Assign one structure to another structure.

You must define each structure with the same number, type, and size of fields (see Using Structures).

Assign one structure to a substructure of a different structure and vice versa.

You must define the structure with the same number, type, and size of fields as the substructure.
Assign an element of one structure to an element of another structure.

The elements must have the same type and size.

Limitations with Structures

The Embedded MATLAB subset supports MATLAB structures with the following limitations to allow efficient code generation in C:

 Add Fields in Consistent Order

 Do Not Assign mxArrays to Structures

 Do Not Add New Fields After First Use of Structures

 Make Structures Uniform in Arrays

 Do Not Reference Fields Dynamically

 Do Not Use Field Values as Constants

 Reference Field Values Individually from Structure Arrays

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS