libstruct - Construct structure as defined in external library

Syntax

s = libstruct('structtype')
s = libstruct('structtype',mlstruct)

Description

s = libstruct('structtype') returns a libstruct object s that is a MATLAB® object designed to resemble a C structure of type specified by structtype. The structure type, structtype, is defined in an external library that must be loaded into MATLAB using the loadlibrary function.

s = libstruct('structtype',mlstruct) returns a libstruct object s with its fields initialized from MATLAB structure, mlstruct.

The libstruct function creates a C-style structure that you can pass to functions in an external library. You handle this structure in MATLAB as you would a true MATLAB structure.

What Types Are Available

To determine which MATLAB types to use when passing arguments to library functions, see the output of libfunctionsview or libfunctions -full. These functions list all of the functions found in a particular library along with a specification of the types required for each argument.

Examples

This example performs a simple addition of the fields of a structure. The function addStructFields is defined in the MATLAB sample shared library, shrlibsample.

Here is the C function:

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

Start by loading the shrlibsample library and creating the structure, sm:

addpath([matlabroot '\extern\examples\shrlib'])
loadlibrary shrlibsample shrlibsample.h

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

Construct a libstruct object sc that uses the c_struct template:

sc = libstruct('c_struct', sm);

get(sc)
    p1: 476
    p2: -299
    p3: 1000

Now call the function, passing the libstruct object, sc:

calllib('shrlibsample', 'addStructFields', sc)
ans =
    1177

You must clear the libstruct object before unloading the library:

clear sc 
unloadlibrary shrlibsample

See Also

loadlibrary, libfunctions, libfunctionsview, libpointer, calllib, libisloaded, unloadlibrary

  


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