Is it possible to pass a function handle to C++ functions in the resent version of matlab?

3 views (last 30 days)
I am going to purchase library compiler toolbox. And before purchasing it, I want to know the way to use it on the standalone project. When I searched for it, I found "How do I pass a function handle to my MATLAB C++ Math Library functions?". (https://kr.mathworks.com/matlabcentral/answers/100819-how-do-i-pass-a-function-handle-to-my-matlab-c-math-library-functions) In this question, "matlab.hpp" and FEVAL_ENTRY are used. However, in the answer to the question "About Matlab C++ Math Library", I can know that they have not supported anymore in the later version of R14. (https://kr.mathworks.com/matlabcentral/answers/36324-about-matlab-c-math-library) In the recent version of matlab, Is it possible to pass a function handle to C++ functions in the resent version of matlab? Thank you.
  1 Comment
Dhruvesh Patel
Dhruvesh Patel on 28 Jun 2017
Edited: Dhruvesh Patel on 28 Jun 2017
The question does not seem to be clear. You wish to buy MATLAB Compiler SDK (Library Compiler App). It is used to generate C/C++ shared library from MATLAB Code.
Do you wish to create a standalone/library from your MATLAB Code or you wish to call a C/C++ function inside MATLAB ?

Sign in to comment.

Answers (1)

Shruthi Mysore Sadashiva
Shruthi Mysore Sadashiva on 3 Aug 2017
I understand that you want to pass function handles to your C++ functions. This could be done by using C++ MEX functions. You can pass function handles to your C++ MEX files and then use “ mexCallMatlab ” function in C++ code to execute the function handles using the FEVAL command.
As an example please find the attached “simplefunction.cpp”, which takes two input arguments and returns an output. The types of input arguments are as follows:
a) A function handle
b) A double value
Please follow these steps to run the example:
1) Use the following command to select C++ compiler:
>>mex -setup
Please note that you have to choose one of the supported and compatible C++ compilers for your version of MATLAB. The following document shows how to choose a C++ compiler: https://www.mathworks.com/help/matlab/matlab_external/choose-c-or-c-compilers.html
To know the supported compilers for your version of MATLAB, please check the following document: https://www.mathworks.com/support/compilers.html
2) Create the MEX file and pass the arguments
>>mex simplefunction.cpp
>>h = @cos;
>>x =0.6;
>>y = simplefunction(h,x);
As Druvesh Patel already mentioned in his comment, MATLAB Compiler SDK is used to generate C/C++ shared library from MATLAB Code. But to pass the function handles to your C++ functions, you should use C++ MEX functions.

Community Treasure Hunt

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

Start Hunting!