1 / 12

第十节 与 C 语言接口

第十节 与 C 语言接口. 1. C 中调用 Matlab. 在 C 语言或 C++ 中可以调用 Matlab 的各种功能,包括各种语句、调用 M 文件或使用工具箱函数。 这就为我们在使用各种 C 或 C++ 语言编程时使用各种 Matlab 内的成熟算法提供了方便。 C 或 C++ 中调用 Matlab 的方法和例子请查看目录: Program FilesMATLAB704extern. 范例: externexampleseng_matengdemo.c externexampleseng_matengwindemo.c 头文件:

suzy
Télécharger la présentation

第十节 与 C 语言接口

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十节 与C语言接口

  2. 1. C中调用Matlab 在C语言或C++中可以调用Matlab的各种功能,包括各种语句、调用M文件或使用工具箱函数。 这就为我们在使用各种C或C++语言编程时使用各种Matlab内的成熟算法提供了方便。 C或C++中调用Matlab的方法和例子请查看目录: \Program Files\MATLAB704\extern

  3. 范例: extern\examples\eng_mat\engdemo.c extern\examples\eng_mat\engwindemo.c 头文件: extern\include\engine.h 库文件: extern\lib\win32\microsoft\msvc60等

  4. 基本函数 1.开启Matlab进程并连接 Engine *engOpen( const char *startcmd ); 2. 关闭连接 int engClose( Engine *ep /* engine pointer */ );

  5. 基本函数 3.获取Matlab变量 mxArray *engGetVariable( Engine *ep, /* engine pointer */ const char *name /* name of variable to get */ ); 4. 将变量导入Matlab int engPutVariable( Engine *ep, /* engine pointer */ const char *var_name, const mxArray *ap /* array pointer */ );

  6. 基本函数 5.执行Matlab语句 int engEvalString( Engine *ep, /* engine pointer */ const char *string /* string for matlab to execute */ );

  7. 基本函数 6.指定存放Matlab输出结果的缓存 int engOutputBuffer( Engine *ep, /* engine pointer */ char *buffer, /* character array to hold output */ int buflen /* buffer array length */ );

  8. 2. VC++中使用Matlab 1. 在VC的包含文件目录中加入: (Matlab目录)\ extern\include 2. 在VC的库文件目录中加入:(Matlab)\extern\lib\win32\microsoft\msvc71 3.在C:\ProgramFiles\MATLAB704 \extern\lib\win32\microsoft\msvc71 目录中找到四个库文件libmex.lib、libmat.lib、libeng.lib、libmx.lib加入到VC程序的Project中

  9. 3. Matlab 中使用C 对于已经存在的C程序,可以通过MEX文件在Matlab中直接调用。 使用MEX文件,可以提高运行速度。 对于AD卡或其他PC硬件,可以通过MEX文件访问。 利用MEX文件,可以使用如Windows用户图形界面等。 在Matlab中,有同名的文件,会先执行MEX文件,其次为DLL文件,最后才是M文件。

  10. 3. Matlab 中使用C 1、安装C语言编译器 mex -setup 2、编译MEX文件 例如:mex yprime.c 会生成yprime.dll 调用 yprime(1,1:2:7)

  11. 4. C语言MEX文件构成 1.入口子程序 void mexFunction( int nlhs, //输出参数数目 mxArray *plhs[], //输出参数指针数组 int nrhs, //输入参数数目 const mxArray*prhs[] //输入参数 ) //指针数组 2.计算功能子程序

  12. 4. C语言MEX文件构成 执行流程: 先从入口子程序mexFunction开始, 从Matlab命令行获取输入矩阵,并赋给prhs, 入口子程序对输入参数进行简单检查后,创建输出矩阵,并赋给plhs; 然后可以在计算功能子程序中使用prhs和 plhs进行计算。

More Related