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

6 ビュー (過去 30 日間)
Seung Ji Lim
Seung Ji Lim 2017 年 6 月 20 日
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 件のコメント
Dhruvesh Patel
Dhruvesh Patel 2017 年 6 月 28 日
編集済み: Dhruvesh Patel 2017 年 6 月 28 日
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 ?

サインインしてコメントする。

回答 (1 件)

Shruthi Mysore Sadashiva
Shruthi Mysore Sadashiva 2017 年 8 月 3 日
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.

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!