This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


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


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


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.

Introduced before R2006a