问题描述
似乎几种方法来调用MATLAB用C C ++和调用C C ++在MATLAB。当我尝试在这里一一列举,请指出来如果我错过了一些东西。
It seems that are several ways to call matlab in C C++ and to call C C++ in matlab. While I try to list them here, please point it out If I miss something.
要调用C C ++在MATLAB中,也有两种方法。第一个是打电话用C共享库中的函数。第二个是要编译C C ++ code成二进制MEX-文件,这些文件将从MATLAB命令行调用。对于第一种方法,是C共享库都只是笼统的,即不改变自己的C code代表MATLAB和一般的C编译器GCC一样编译?
To call C C++ in matlab, there are also two methods. The first one is to call functions in C shared libraries. The second one is to build C C++ code into binary MEX-files, which will be called from the MATLAB command line. For the first method, are the C shared libraries are just general ones, i.e. without change to their C code for matlab and compiled from general C compiler like gcc?
要调用Matlab code用C C ++中,有两种方法可用。第一个是Matlab引擎。第二个是使用MATLAB编译器MCC从您的MATLAB code创建C或C ++共享库。
To call matlab code in C C++, there are two methods available. The first one is Matlab engine. The second one is to use MATLAB Compiler mcc to create C or C++ shared libraries from your MATLAB code.
除了MATLAB和C C ++可以通过读写数据通信和一些文件。
Besides matlab and C C++ can communicate via writing and reading data to and from some file (e.g. mat file, text file).
有一个以上的方式来完成这里的每一个目标,你能告诉我什么情况下最适合使用的人?即调用C函数的共享库VS大厦C C ++ code成二进制MEX-文件,Matlab引擎VS编译Matlab的code为C C ++共享库。
Having more than one ways to accomplish each of the goals here, could you tell me what cases are best for using which of them? i.e. calling functions in C shared libraries VS building C C++ code into binary MEX-files, Matlab engine VS compiling Matlab code into C C++ shared library.
感谢和问候!
推荐答案
我只有前preience从MATLAB调用C或C ++函数。这在我看来就像调用函数在共享库,并从MEX文件调用函数之间的唯一区别是,共享库,你必须调用与'calllib',这是一个命令行输入功能,MEX功能允许功能您调用函数,就好像它们是内置的功能,使界面干净了一点。
I only have expreience with calling C or C++ functions from MATLAB. It looks to me like the only difference between calling functions in a shared library and calling functions from a MEX file is that with a shared library, you have to call the function with 'calllib' which is a command line type function and MEX functions allow you to call functions as if they are built-in functions so the interface is a little cleaner.
我的建议是使用MEX文件,如果
My suggestion is to use MEX files if
- 您使用的是C ++(您可能需要编写一个包装在一个共享库中使用C ++)
- 您正在使用MATLAB的胶水进行了大量优化的C或C ++程序的。你要能够干净利落打电话给他们。
使用共享库,如果
- 您已经拥有了可以不加修改地使用现有的C库。
- 您只需要少量的调用C函数的。
真的,它涉及到的接口。我个人preFER的MEX文件路径,因为它提供了从MATLAB最干净的界面,C或C ++函数。你可以把它像标准MATLAB类型只是一个功能。随着共享库,你可以在调用库函数前做一些数据格式
Really, it comes down to the interface. I personally prefer the MEX file route because it provides the cleanest interface from MATLAB to your C or C++ function. You can call it like just another function with standard MATLAB types. With a shared library, you may have to do some data formatting before calling the library function
这篇关于MATLAB用C C ++和C C ++在MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!