需要学习在matlab中调用C++,记录一下看的资料,方便后期查阅
参考文章:https://www.likecs.com/show-308599183.html
matlab中的的c++编程称为mex编程,也就是说,先写好 C++文件,然后在matlab中调用它。
具体步骤:
(1)在matlab命令行中 输入"mex -setup" 就会有相应的提示
(2)编写C++文件,假如文件名为test.cpp
#include "mex.h"
void mexFunction(int nlhs,mxArray* plhs[],int nrhs,mxArray* rhs[])
{
printf("hello world!");
}
(3)使用mex test.cpp (文件名,要在同一路径下),可以编译这个文件
编译完成之后可以看到在matlab中,同目录下生成了一个同名的.mexw32
(4)然后在matlab命令中输入test(),就会输出结果
【之所以要用test(),是因为文件名是 test.cpp 】
mex源文件编译完成之后在matlab里当函数调用的。
记录:刚刚尝试了一下,有参数的情况,参考文档写的不是很明白,所以耽误了很长时间,这边记录一下
me(a) 【a是参数,me是c++文件名】
mex -setup
mex me.cpp "-g";
a = [1.1,2.1,3;4,5,6;7,8,9];
a=single(a)
me(a)