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 libname
is
libnameInterface.
, where
ext
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
Call addpath
on the folder containing the interface file.
Display Help
The MATLAB
help
and doc
functions provide help for members of
the library. For example, to display help for function funcname
in
library libname
, type:
help clib.libname.funcname
Call Function
To call a function funcname
in C++ library libname
with input arguments arg1,arg2,...
and output argument
retVal
, use the MATLAB
clib
namespace. 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
clib
namespace.
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 (
*
)std::vector
MATLAB Argument (Scalar or Array) | C++ Parameter Type (Scalar or
Array) Closest Type <———————————————————————> Least Close Type | ||||||
---|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
| ||
|
|
|
|
| |||
complex |
|
|
|
|
|
| |
complex |
|
| |||||
complex |
|
|
|
|
|
| |
complex |
|
|
|
|
| ||
complex |
|
|
|
| |||
complex |
|
| |||||
|
|
| |||||
| enum | ||||||
|
|
| |||||
MATLAB object of C++ type | C++ object of type | ||||||
MATLAB object of MATLAB class | Unsupported |
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 arg
is 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:
clib.libname.funcname clib.libname.funcname(100)
This statement is also correct, although your result might be different:
clib.libname.funcname(99)
MATLAB supports default arguments for scalar integer and floating point types.
Call Function with Enum Arguments
You can pass a string to a C++ function or method in C++ library
libname
that accepts
clib.
as an input
argument.libname
.enum
For example, suppose that you have a MATLAB interface to C++ library libname
with a function
getValue
that takes clib.libname.Days
as
input:
help clib.libname.getValue
getValue - clib.libname.getValue Representation of C++ function getValue. RetVal = clib.libname.getValue(D) Input Arguments D clib.libname.Days Output Arguments RetVal int32
To display the values for clib.libname.Days
, type:
doc clib.libname.Days
Enumeration Summary Fri Mon Sat Sun Thu Tue Wed
You can call getValue
with either
clib.libname.Days.Sun
or the string "Sun"
.
clib.libname.getValue(clib.libname.Days.Sun)
clib.libname.getValue("Sun")
ans = 106
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.