我只有从 MATLAB 调用 C 或 C++ 函数的经验。在我看来,调用共享库中的函数和从 MEX 文件调用函数之间的唯一区别是,使用共享库时,您必须使用“calllib”调用该函数,这是一个命令行类型函数,并且 MEX 函数允许您可以像调用内置函数一样调用函数,这样界面就更简洁了。
我的建议是,如果
您使用 C++(您可能需要编写一个包装器才能在共享库中使用 C++),
请使用 MEX 文件。您正在使用 MATLAB 作为大量优化的 C 或 C++ 例程的粘合剂。您会希望能够干净地调用它们。
请使用共享库
如果您已有无需修改即可使用的现有 C 库,
。您只需要少量调用 C 函数。
实际上,这取决于界面。我个人更喜欢 MEX 文件路径,因为它提供了从 MATLAB 到 C 或 C++ 函数的最简洁的接口。您可以像调用另一个具有标准 MATLAB 类型的函数一样调用它。使用共享库,您可能需要在调用库函数之前进行一些数据格式化
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.
My suggestion is to use MEX files if
You are using C++ (you may have to write a wrapper to use a C++ in a shared library)
You are using MATLAB as the glue for a large number of optimized C or C++ routines. You'll want to be able to call them cleanly.
Use shared library if
You already have an existing C library that can be used without modification.
You only need a small number of calls to C functions.
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
如果您编写自己的 S-Function 或 matlab 扩展,那么从 matlab 调用 c 就是您想要做的事情。
C 和 C++ 之间的选择取决于您;如果你从一张白纸开始,我建议你使用 C++;您不需要使用完整的功能,但它提供了更多的自由。现在也有更多的库可用于 C++。
如果您需要迁移到截然不同的环境,C 是首选语言;例如,将 C 语言编译为 DSP。或者如果您有 C 语言的遗留代码可以开始。混合 C 和 C++ 是可能的,但可能有点麻烦;我确信您会在 StackOverflow 上找到仅与此主题相关的主题。
I think the methods you've named are correct (it's been a while since I've used them)
The matlab C-compiler isn't really special; it is possible to use different compilers. See link list of supported compilers. This does not include gcc, but MS Visual studio is included. You'll run into issues when linking with the supplied libraries.
Basically: calling matlab from C is something you'd do if you need a tight interface; for instance if you want to synchronise 2 tools, or your S-function (simulink) requires additional information. But then, such a file is propably called by Matlab/simulink in the first place.
Calling c from matlab is what you want to do if you write your own S-functions or extensions to matlab.
The choice between C and C++ is yours; if you start from a blank sheet I suggest you use C++; you don't need to use the complete functionality but it allows more freedom. Also more libraries tend to be available for C++ nowadays.
C is the language of choice if you need to migrate to very different environments; i.e. to compile C to DSPs for instance. Or if you have got legacy code in C to start from. Mixing C and C++ is possible, but a can be a bit cumbersome; I'm sure you'll find topics on StackOverflow on this subject alone.
发布评论
评论(2)
我只有从 MATLAB 调用 C 或 C++ 函数的经验。在我看来,调用共享库中的函数和从 MEX 文件调用函数之间的唯一区别是,使用共享库时,您必须使用“calllib”调用该函数,这是一个命令行类型函数,并且 MEX 函数允许您可以像调用内置函数一样调用函数,这样界面就更简洁了。
我的建议是,如果
请使用共享库
实际上,这取决于界面。我个人更喜欢 MEX 文件路径,因为它提供了从 MATLAB 到 C 或 C++ 函数的最简洁的接口。您可以像调用另一个具有标准 MATLAB 类型的函数一样调用它。使用共享库,您可能需要在调用库函数之前进行一些数据格式化
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.
My suggestion is to use MEX files if
Use shared library if
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 编译器并不是很特别;可以使用不同的编译器。请参阅支持编译器的链接列表。这不包括 gcc,但包括 MS Visual studio。与提供的库链接时您会遇到问题。
基本上:如果您需要一个紧密的接口,您可以从 C 调用 matlab;例如,如果您想要同步 2 个工具,或者您的 S-Function (simulink) 需要其他信息。但是,这样的文件很可能首先由 Matlab/simulink 调用。
如果您编写自己的 S-Function 或 matlab 扩展,那么从 matlab 调用 c 就是您想要做的事情。
C 和 C++ 之间的选择取决于您;如果你从一张白纸开始,我建议你使用 C++;您不需要使用完整的功能,但它提供了更多的自由。现在也有更多的库可用于 C++。
如果您需要迁移到截然不同的环境,C 是首选语言;例如,将 C 语言编译为 DSP。或者如果您有 C 语言的遗留代码可以开始。混合 C 和 C++ 是可能的,但可能有点麻烦;我确信您会在 StackOverflow 上找到仅与此主题相关的主题。
I think the methods you've named are correct (it's been a while since I've used them)
The matlab C-compiler isn't really special; it is possible to use different compilers. See link list of supported compilers. This does not include gcc, but MS Visual studio is included. You'll run into issues when linking with the supplied libraries.
Basically: calling matlab from C is something you'd do if you need a tight interface; for instance if you want to synchronise 2 tools, or your S-function (simulink) requires additional information. But then, such a file is propably called by Matlab/simulink in the first place.
Calling c from matlab is what you want to do if you write your own S-functions or extensions to matlab.
The choice between C and C++ is yours; if you start from a blank sheet I suggest you use C++; you don't need to use the complete functionality but it allows more freedom. Also more libraries tend to be available for C++ nowadays.
C is the language of choice if you need to migrate to very different environments; i.e. to compile C to DSPs for instance. Or if you have got legacy code in C to start from. Mixing C and C++ is possible, but a can be a bit cumbersome; I'm sure you'll find topics on StackOverflow on this subject alone.