Documentation

Call C Shared Libraries

Directly call C library functions from MATLAB®

Functions

loadlibrary Load C/C++ shared library into MATLAB
unloadlibrary Unload shared library from memory
libisloaded 确定共享库是否已加载
Calllib Call function in shared library
libfunctions Return information on functions in shared library
libfunctionsview Display shared library function signatures in window
libstruct Convert MATLAB structure to C-style structure for use with shared library
libpointer Pointer object for use with shared library

Classes

lib.pointer Pointer object compatible with C pointer

Examples and How To

Shared Library shrlibsample

MATLAB includes a sample external library calledshrlibsample.

Pass String Arguments

ThestringToUpperfunction in theshrlibsamplelibrary converts the characters in the input argument to uppercase.

Pass Structures

Theshrlibsampleexample library contains two functions withc_structstructure input parameters.

Explore libstruct Objects

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

Pass Enumerated Types

TheReadenumfunction in theshrlibsamplelibrary displays a string that matches the input argument.

Pass Pointers

ThemultDoubleReffunction in theshrlibsamplelibrary multiplies the input by5.

Pass Arrays

Theprint2darrayfunction in theshrlibsamplelibrary 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.

Concepts

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原型文件, a file of MATLAB commands.

Troubleshooting

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.

不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?