Main Content

Pass Functions as Arguments

MATLAB® supports C++ signatures with std::function and C function pointer arguments. Both std::function and C function pointer are function types in MATLAB. A function type is the type of the parameter to which a function can be assigned. This means that you can pass a MATLAB function as an argument to a C++ function.

Note

MATLAB does not support C function pointers and std::function as function return types or data members.

Display Help for Interface

Suppose that you have an interface that defines a function that manipulates an array with a function defined by a funcPtrType. To run this example, follow the instructions in Build Interface for Function Argument Example to generate a MATLAB interface named libFunctionArg.

help clib.libFunctionArg
help clib.FunctionArg.double_inputoutput
 clib.libFunctionArg.double_inputoutput Representation of C++ function double_inputoutput.

  arr = clib.libFunctionArg.double_inputoutput(arr)
    Input Arguments
      arr            vector int32  

    Output Arguments
      arr            vector int32  

Pass C++ Library Function

You can use the double_inputoutput function defined in this header file to pass as input to function type argument in callFunc_inputoutput to manipulate an array. You can do this by creating a MATLAB function handle to double_inputoutput.

Call callFunc_inputoutput with the double_inputoutput function.

fp = @clib.libFunctionArg.double_inputoutput;
arr = [1,2,3,4,5,6];
clib.libFunctionArg.callFunc_inputoutput(arr,fp)

Choose Input Function to Function Type

The MATLAB help function on function type displays a list of functions in your library that are compatible with your function type.

help clib.libFunctionArg.funcPtrType
Accepted input for clib.libFunctionArg.funcPtrType is a handle to a C++ library function 
with matching signature.
C++ library functions with matching inputs and outputs to the C++ function type:
@clib.libFunctionArg.double_inputoutput

Build Interface for Function Argument Example

This C++ code defines a function double_inputoutput that manipulates an array with a function defined by a funcPtrType.

#include <functional>
void double_inputoutput( int * arr, int len){
       for(int i=0;i<len;++i){
               arr[i] = arr[i]*2;
       }
}
using funcPtrType = std::function<void(int*,int)>;
void callFunc_inputoutput(int *arr, int len , funcPtrType fp){
       fp(arr, len);
}

To run the example, save this code in a header file named functionArg.hpp, then generate a MATLAB interface named libFunctionArg following the instructions in Header-Only HPP File.

Library ArtifactsMATLAB Interface libnameMATLAB Help

Header file functionArg.hpp

clib.libFunctionArg

>> help clib.libFunctionArg

Declaring Function Types in C++

These C++ code statements show examples of declaring function types.

  • Type declared in a typedef statement—This code declares ACallback as a function passed to createTrackbar.

    #include <string>
    
    typedef void (*ACallback)( 
        int pos, 
        void *data 
        ); 
    
    int createTrackbar(const std::string& tname, ACallback onChange=0, void* data=0){
        };
  • Type declared in a using statement—This code declares funcPtrType as a function passed to task.

    #include <functional>
    
    using funcPtrType = std::function<void(int*,int)>;
    void task(int *arr, int len , funcPtrType fp){
        fp(arr, len);
    }
  • Function pointer type with no typedef statement—MATLAB generates names to represent these function pointers. Auto-generated names start with the prefix Function followed by a numeric suffix. The new types are added to the clib.type.libname namespace. This code declares task1 and task2 as function pointers.

    void task1((void (*param)(int));
    void task2((void (*param)(long));

    The MATLAB signatures for the functions are:

    clib.libname.task1(clib.type.libname.Function1)
    clib.libname.task2(clib.type.libname.Function2)

See Also