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.

Call C Shared Libraries

Directly call C library functions from MATLAB®


loadlibraryLoad C/C++ shared library into MATLAB
unloadlibraryUnload shared library from memory
libisloadedDetermine if shared library is loaded
calllibCall function in shared library
libfunctionsReturn information on functions in shared library
libfunctionsviewDisplay shared library function signatures in window
libstructConvert MATLAB structure to C-style structure for use with shared library
libpointerPointer object for use with shared library


lib.pointerPointer object compatible with C pointer

Examples and How To

Shared Library shrlibsample

MATLAB includes a sample external library called shrlibsample.

Pass String Arguments

The stringToUpper function in the shrlibsample library converts the characters in the input argument to uppercase.

Pass Structures

The shrlibsample example library contains two functions with c_struct structure input parameters.

Explore libstruct Objects

This example shows how to display information about and modify a libstruct object, c_struct.

Pass Enumerated Types

The readEnum function in the shrlibsample library displays a string that matches the input argument.

Pass Pointers

The multDoubleRef function in the shrlibsample library multiplies the input by 5.

Pass Arrays

The print2darray function in the shrlibsample library displays the values of a 2-D array with three columns and a variable number of rows.

Iterate Through an Array

This example shows how to create a MATLAB® cell array of character vectors, mlStringArray, from the output of the getListOfStrings function.


Call Functions in Shared Libraries

How to call functions in external, shared libraries from MATLAB software.

Pass Arguments to Shared Library Functions

How to construct MATLAB arguments compatible with the argument types found in the library functions.

Structure Arguments

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

Pointer Arguments

Many functions in external libraries pass arguments by reference.

MATLAB Prototype Files

MATLAB provides a way to modify header file information by creating a prototype file, a file of MATLAB commands.


Limitations to Shared Library Support

The MATLAB shared library interface supports C library routines only.

Limitations Using Structures

MATLAB returns pointers to structures.

Loading Library Errors

Errors occur when the shared library is not a valid library.

No Matching Signature Error

This error occurs when you call a function without the correct input or output arguments, or if there is an error in the function signature in the header file.

MATLAB Terminates Unexpectedly When Calling Function in Shared Library

Some shared libraries, compiled as Microsoft® Windows® 32-bit libraries, use a calling convention that is incompatible with the default MATLAB calling convention.

Was this topic helpful?