Using the C Math Library    

Creating Structures

The MATLAB C Math Library allows you to create structures by:

Using a Structure Creation Routine

You can create a structure using the mlfStruct() routine. This routine lets you define the fields in the structure and assign a value to each field. For example, the following code fragment creates a structure that contains two fields, a text string and a scalar value.

This code produces the following output:

Because the mlfStruct() routine can accept a varying number of input arguments, you must terminate the argument list with a NULL.

Creating Multidimensional Arrays of Structures

The mlfstruct() routine defines the fields and values in a single instance of a structure, in effect a 1-by-1 structure array. To create a multidimensional array of structures, use MATLAB indexing to assign a value to a field in a structure with an index other than (1,1). MATLAB will extend the array of structures to accommodate the location specified. For more information about using assignment with structures, see Using Assignment to Create Structures.

Using a Structure Conversion Routine

You can also create structures by converting an existing MATLAB cell array into a structure, using the mlfCell2struct() routine. This example creates a cell array to be converted, and a second cell array that specifies the names of the fields in the structure. You pass these two cell arrays, along with the dimensions of the structure array, as arguments to mlfCell2struct().

Note that, because mlfCellhcat() accepts a variable number of input arguments, you must terminate the input argument list with a NULL.

This code generates the following output.

Using Assignment to Create Structures

You can also create a structure by assigning a value to a location in a structure, using the mlfIndexAssign() routine. The MATLAB C Math Library creates a structure (or array of structures) large enough to accommodate the location specified by the index string. For more information about structure indexing, see Chapter 5.

The following example is equivalent to the MATLAB statement,
A(2) = struct('name','jim','number',312).

The following output shows the structure created by this code fragment.

For more detailed information about using mlfIndexAssign() to assign values to fields in a structure, see Chapter 5.


 MATLAB Structures Performing Common Array Programming Tasks