Main Content


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


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


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


collapse all

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

Load the shrlibsample library in the matlabroot\extern\examples\shrlib folder, which contains the c_struct type.

if ~libisloaded('shrlibsample')

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.

ans =

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

clear sc
unloadlibrary shrlibsample

Input Arguments

collapse all

C structure defined in shared library.

MATLAB structure used to initialize the fields in S.

Data Types: struct

Output Arguments

collapse all

Pointer, returned as MATLAB libstruct object.


  • 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.


  • 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.

Version History

Introduced before R2006a