How do I pass a function handle to my MATLAB C++ Math Library functions?

2 views (last 30 days)
Some functions in the MATLAB C/C++ Math Library require function handles as inputs to functions. In the MATLAB C/C++ Math Library documentation, these type of functions are called function-functions. Their MATLAB counterparts are functions which require inputs of the form "@function". How do I create function handles to provide as inputs to these function-functions in C/C++?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
In order to pass a function handle into your function, you must first register your function in the function look-up table. This is discussed in the C++ Math Library Users Guide.
Here is a simple example code that illustrates how to add the function "func" to the function table look-up.
#include "matlab.hpp"
mwArray func(mwArray x) /*Function that will be passed as a handle to another function*/
{
return sin(x);
}
DECLARE_FEVAL_TABLE /*Macro that adds the function func into the FEVAL table*/
FEVAL_ENTRY(func)
END_FEVAL_TABLE
int main(void)
{
mwArray x;
mwArray x0(2);
cout<<x0<<"\n"<<"\n"<<endl;
x = fzero("func",x0); /*func is passed to the function FZERO as a function handle*/
cout<<x<<endl;
return 0;
}

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!