Documentation

libstruct

Convert MATLAB structure to C-style structure for use with shared library

Syntax

  • S = libstruct(structtype)
  • S = libstruct(structtype,mlstruct) example

Description

S = libstruct(structtype) creates NULL pointer to MATLAB® libstruct object S.

example

S = libstruct(structtype,mlstruct) creates pointer initialized to mlstruct.

Examples

expand all

Call Function with c_struct Input Argument

Call the addStructFields function by creating a variable of type c_struct.

Load the shrlibsample library, which contains the c_struct type.

if ~libisloaded('shrlibsample')
   addpath(fullfile(matlabroot,'extern','examples','shrlib'))
   loadlibrary('shrlibsample')
end

Display function signatures for shrlibsample and search the list for the addStructFields entry.

libfunctionsview shrlibsample
double addStructFields(c_struct)

The input argument is a pointer to a c_struct data type.

Create a MATLAB structure, sm.

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

Construct a libstruct object sc from the c_struct type.

sc = libstruct('c_struct',sm)

The fields of sc contain the values of the MATLAB structure, sm.

Call the addStructFields function.

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

To clean up, first clear the libstruct object, and then unload the library.

clear sc
unloadlibrary shrlibsample

Input Arguments

expand all

structtype — C structurestructure

C structure defined in shared library.

mlstruct — MATLAB structurestructure

MATLAB structure used to initialize the fields in S.

Data Types: struct

Output Arguments

expand all

S — PointerMATLAB libstruct object

Pointer, returned as MATLAB libstruct object.

Limitations

  • Use with libraries that are loaded using the loadlibrary function.

  • You can only use the libstruct function on scalar structures.

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

More About

expand all

Tips

  • If a function in the shared library has a structure argument, use libstruct to create the argument. The libstruct function creates a C-style structure that you pass to functions in the library. You handle this structure in MATLAB as you would a true MATLAB structure.

Was this topic helpful?