How to execute C++ scripts and functions which I need to call from MATLAB (2019a) scripts/functions?

1 次查看(过去 30 天)
I have successfully installed MinGW-w64 C/C++ compiler as add-on and executed the command 'mex -setup'. I have kept the folder contains C++ files in the current path. What next to do? This is the first time I am working with mex. Thanks for any help in advance.

回答(1 个)

Chidvi Modala
Chidvi Modala 2020-6-12
To create and use a MEX file, you may need to:
  • Determine and install a supported compiler for your MATLAB release.
  • Execute 'mex -setup' command to configure visual studio compiler for C/C++ compilation.
  • Use the MATLAB ‘mex’ command to build a binary MEX file from your C/C++ source files.
  • Use your binary MEX file like any MATLAB function.
You can use the following commands to obtain a copy of the file, ‘timestwo.c’ on your local installation of MATLAB, and create a MEX file from it:
copyfile(fullfile(matlabroot,'extern','examples','refbook','timestwo.c'),'.','f')
mex timestwo.c
Upon successful creation of a MEX file, you should see something like:
Building with 'MinGW64 Compiler C '.
MEX completed successfully.
You can refer to this documentation
  2 个评论
Surama Biswas
Surama Biswas 2020-6-12
@Chidvi Modala: Thank you very much for your response. Will you please elaborate the point ("Use your binary MEX file like any MATLAB function") with this example?
Chidvi Modala
Chidvi Modala 2020-6-15
After generating MEX file, we can call the mex function by calling its name and passing input arguments.
For the above provided example, You can run the generated mex by executing following command
timestwo(2) % Input argument can be any value

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by