Call Functions in C++ Compiled Library - MATLAB & Simulink (original) (raw)

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 MATLABAdd-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 islibnameInterface._`ext`_, where_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 MATLABhelp 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

Choose from Overloaded Functions

For overloaded functions, MATLAB chooses the function with the input data type that is closest to the given data type, based on MATLAB Type to C++ Type Mapping.

Call Function

To call a function funcname in C++ library libname with input arguments arg1,arg2,... and output argumentretVal, use the MATLABclib 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 theclib namespace.

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:

help clib.libname.funcname

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 acceptsclib._`libname`_.enum as an input argument.

For example, suppose that you have a MATLAB interface to C++ library libname with a functiongetValue 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:

Enumeration Summary Fri Mon Sat Sun Thu Tue Wed

You can call getValue with eitherclib.libname.Days.Sun or the string "Sun".

clib.libname.getValue(clib.libname.Days.Sun) clib.libname.getValue("Sun")

See Also

Topics