Call Functions in C++ Compiled Library
The publisher of your MATLAB® interface to a C++ library provides you with instructions for installing the interface file and any dependent library files, if required. The publisher might give you dependent library files, ask you to install libraries from an external source, or provide a link to all relevant files. If the publisher created a toolbox using MATLAB Add-Ons, then this information might be found in the Getting Started Guide available through the Options menu for the toolbox in the Add-On Manager. If you need more information or cannot find a Getting Started Guide, then contact the publisher. For details about add-ons, see Manage Add-Ons.
The name of the interface file for library
ext is platform-specific —
.dll on Windows®,
.so on Linux®, or
.dylib on macOS.
Set Run-Time Path
MATLAB looks for the interface file on the MATLAB path and the dependent library files on the system path or run-time search path (rpath). If the publisher gives you dependent library files, you can put them in the same folder as the interface file. Alternatively, to add them to the system path, see Set Run-Time Library Path for C++ Interface. For information about locating dependent libraries, see Missing or Incorrectly Installed Run-Time Libraries.
Set MATLAB Path
addpath on the folder containing the interface file.
doc functions provide help for members of
the library. For example, to display help for function
To call a function
funcname in C++ library
with input arguments
arg1,arg2,... and output argument
retVal, use the MATLAB
clib package. MATLAB automatically loads the library when you type:
retVal = clib.libname.funcname(arg1,arg2,...)
After MATLAB loads the library, you can use tab completion to view the members of the
MATLAB Type to C++ Type Mapping
When you pass MATLAB data as arguments to C++ methods or functions, MATLAB converts the data into types that best represent the data to the C++ language.
Each row in this table shows a MATLAB type followed by the possible C++ argument matches, from left to right in order of closeness of the match. The MATLAB types (except cell arrays) can be scalar (1-by-1) arrays or matrices. The C++ types can be scalar values or arrays. The phrase cpparray refers to these C++ array types:
box array (
array buffer (
|MATLAB Argument (Scalar or Array)|
C++ Parameter Type (Scalar or
Closest Type <———————————————————————> Least Close Type
MATLAB object of C++ type
C++ object of type
MATLAB object of MATLAB class
Call Function with Default Arguments
If a C++ function is defined with default arguments, then you can call the function
without providing one or more trailing arguments. The function help shows the default value.
For example, if the type of
double and its
default value is
100, then help displays:
clib.libname.funcname(arg) Input Arguments arg double = 100
These statements produce the same result:
This statement is also correct, although your result might be different:
MATLAB supports default arguments for scalar integer and floating point types.
Access Violation Errors
When you make calls to the library, make sure that your library was compiled in Release mode, not Debug mode. For help with other common problems, see Troubleshooting MATLAB Interface to C++ Library Issues.