如何用c++代码调用matlab自定义函数

How to call matlab self defined function with c++ code

如果我在 matlab 文件 add.m 中定义 add(a,b) 函数,我如何从 C++ 代码中调用它? 一个简单的例子是这样的: add.m:

function y=add(a,b)
y=a+b;
end

C++代码:

int main(){
// call add(a,b) ?
}

实际上,我需要在我的 cpp 代码中使用更复杂的 matlab 函数,例如 cp2tform()imtransform()

您可以从 matlab via this 生成 C 代码。并从您的主要功能调用。

您可以从 C/C++ 程序中调用 MATLAB 引擎。参见例如external interfaces reference R2016b or the online documentation on the MATLAB Engine。在 C/C++ 代码中使用 Matlab 引擎时,您只需在使用 engPutVariable 命令将 ab 传递给 Matlab 后调用 engEvalString(MyEngine,"add(a,b)"); .