Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Working with Structures

Structure Argument Requirements

When you pass a MATLAB structure to an external library function:

You do not need to match the data types of numeric fields. The calllib function converts to the correct numeric type.

Working with Structures Examples

[instructions for opening shrlibsample code]

Examples in this topic are:

Finding Structure Field Names

To determine the name and data type of structure fields, you can:

Example of Finding Structure Field Names

You can determine the field names of an externally defined structure using the libstruct function. For example, look at the addStructFields function in the shrlibsample library. It has the signature:

double addStructFields (c_struct)

Create a libstruct object:

s = libstruct('c_struct');

To get the names of the fields, type:

get(s)

MATLAB displays the field names and their values:

    p1: 0 
    p2: 0 
    p3: 0

To set the field values, type:

s.p1 = 476;   s.p2 = -299;   s.p3 = 1000;
calllib('shrlibsample','addStructFields',s);
get(s)

MATLAB displays:

    p1: 476
    p2: -299
    p3: 1000

Example of Passing a MATLAB Structure

The following example passes a MATLAB structure to the addStructFields function in the shrlibsample library. The library defines the following:

struct c_struct {
    double p1;
    short p2;
    long  p3;
};

double addStructFields(struct c_struct st) {
    double t = st.p1 + st.p2 + st.p3;
    return t;
}

To load the library, type:

if not(libisloaded('shrlibsample'))
  addpath(fullfile(matlabroot , 'extern', 'examples', 'shrlib'));
  loadlibrary shrlibsample shrlibsample.h;
end

Create a structure, sm, with three fields of type double:

sm.p1 = 476;   sm.p2 = -299;   sm.p3 = 1000;

The calllib function converts the fields to the double, short, and long data types defined in c_struct:

calllib('shrlibsample', 'addStructFields', sm)

MATLAB displays:

ans =
        1177

Passing a libstruct Object

When working with small structures, you can let MATLAB convert the structure being passed to the library definition for that structure type, as described in Structure Argument Requirements. However, when working with repeated calls that pass one or more large structures, it may be to your advantage to convert the structure manually before making any calls to external functions. In this way, you save processing time by converting the structure data only once at the start rather than at each function call. You can also save memory if the fields of the converted structure take up less space than the original MATLAB structure. You do this by creating a libstruct object, as described in the following topics:

Preconverting a MATLAB Structure with libstruct

Use the libstruct function to convert a MATLAB structure to a C-style structure. The syntax for libstruct is:

s = libstruct('structtype', mlstruct)

The variable s is a libstruct object. Although it is an object, it behaves like a MATLAB structure. The fields of the object are derived from the external structure type specified by structtype.

For example, to convert a MATLAB structure, sm, to a libstruct object, sc, type:

sm.p1 = 476;   sm.p2 = -299;   sm.p3 = 1000;
sc = libstruct('c_struct', sm);

All of fields in the original structure sm are of type double. The object sc has fields that match the c_struct structure type. These fields are double, short, and long. Type:

get(sc)

MATLAB displays:

    p1: 476
    p2: -299
    p3: 1000

Creating an Empty libstruct Object

To create an empty libstruct object, call libstruct with only the structtype argument. For example:

sci = libstruct('c_struct')
get(sci)

MATLAB displays the initialized values:

    p1: 0
    p2: 0
    p3: 0

libstruct Requirements for Structures

When converting a MATLAB structure to a libstruct object, the structure must adhere to the requirements listed in Structure Argument Requirements.

Example of Passing a libstruct Object

Compare the following example with the Example of Passing a MATLAB Structure. Convert structure sm to type c_struct:

sm.p1 = 476;   sm.p2 = -299;   sm.p3 = 1000;
sc = libstruct('c_struct', sm);
get(sc)

MATLAB displays:

    p1: 476
    p2: -299
    p3: 1000

Now call the function with the libstruct object sc:

calllib('shrlibsample', 'addStructFields', sc)

MATLAB displays:

ans =
    1177

Using the Structure as an Object

A libstruct object is not a MATLAB structure. It is an instance of a class called lib.c_struct. Type:

whos

MATLAB displays (in part):

  Name      Size                   Bytes  Class

sc         1x1                           lib.c_struct
sm         1x1                      396  struct array

Determining the Size of a libstruct Object

You can use the lib.c_struct class method structsize to obtain the size of a libstruct object:

sc.structsize

MATLAB displays:

ans = 
		16

Accessing Fields of a libstruct Object

The fields of this structure are properties of the lib.c_struct class. You can read and modify any of these fields using the MATLAB object-oriented functions, set and get:

sc = libstruct('c_struct');
set(sc, 'p1', 100, 'p2', 150, 'p3', 200);
get(sc)

MATLAB displays:

    p1: 100
    p2: 150
    p3: 200

You can read and modify the fields by treating them like MATLAB structure fields:

sc.p1 = 23;
sc.p1

MATLAB displays:

ans =
   23
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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