Main Content

Use Function Type 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. C function pointers and std::function are not supported as function return types or data members.

Function Types

  • If a type in the library is declared in a typedef statement, then you can use it as a function type. For example, suppose that you build this C++ code into an interface libname.

    typedef void (*ACallback)(int pos, void *data);
    int createTrackbar(const string& tname, ACallback onChange=0, void* data=0);

    You can specify clib.libname.ACallback as a function type. The MATLAB signature for createTrackbar is:

    int32 clib.libname.createTrackbar(string tname, clib.libname.ACallback onChange, int32 data)
  • If a type in the library is declared in a using statement, then you can use it as a function type. For example, suppose that you build this C++ code into an interface libname.

    using funcPtrType = std::function<void(int*,int)>;
    void task(int *arr, int len , funcPtrType fp){
        fp(arr, len);
    }

    You can specify clib.libname.funcPtrType as a function type. The MATLAB signature for task is:

    clib.libname.task(clib.array.libname.Int,clib.libname.funcPtrType)
  • clib.type.libname.FunctionN, where N is 1 for the first function type without a typedef and incremented for additional function types in libname. For example, libname contains these functions:

    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)

Call C++ Functions With Function Type Input

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.

#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);
}

Call callFunc_inputoutput with the double_inputoutput function.

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

Help for Function

help clib.test2.double_inputoutput
 clib.test2.double_inputoutput    Representation of C++ function double_inputoutput.

    Inputs
      arr            int32  

    Outputs
      arr            int32  

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.test2.funcPtrType
Accepted input for clib.test2.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.test2.double_inputoutput

See Also