Matlab 与 CCCC 混合编程汇总
Matlab
Matlab
Matlab
1.1.1.1. Matlab
Matlab
Matlab 调用 CCCC
Matlab
1)1)1)1) MEXMEXMEXMEX方法(注:Matlab
Matlab
Matlab自带的LCCLCCLCCLCC不支持中文注释)
Matlab
/////////////////////////////////////////////////////////////////
原C程序:
// add.h
#ifndef LIB_H
#define LIB_H
extern "C" int add(int x,int y);
#endif
//add.c
#include "add.h"
int add(int x,int y)
{
//声明为C编译、连接方式的外部函数
return x + y;
}
/////////////////////////////////////////////////////////////////
改写后的 MEX 程序:
//完整的add.c如下:
#include "add.h"
#include "mex.h" // 使用MEX文件必须包含的头文件
// 执行具体工作的C函数
double add(double x, double y)
{
return x + y;
}
// MEX文件接口函数
void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[])
{
*a;
double
double b, c;
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
a =
b = *(mxGetPr(prhs[0]));
c = *(mxGetPr(prhs[1]));
*a
mxGetPr(plhs[0]);
= add(b, c);
}
/////////////////////////////////////////////////////////////////
编译: mex add.c
2222)DLLDLLDLLDLL方法(CCCC生成)
/////////////////////////////////////////////////////////////////
打开 Vs2010,文件->新建->项目->Win32 项目->应用程序类型选择 DLL,输入以下代码:
//add.h
#ifndef LIB_H
#define LIB_H
extern "C" int __declspec(dllexport)add(int x, int y);
#endif
//add.c
#include "add.h"
int add(int x,int y)
{
return x + y;
}
/////////////////////////////////////////////////////////////////
% Matlab调用DLL
% 用loadlibrary函数, 根据add.h中的函数定义,加载add.dll
loadlibrary('add.dll','add.h');
% 查看add.dll支持的函数接口
libfunctions add -full
% int32 add(int32, int32)
out=calllib('add','add',23,32);
unloadlibrary('add')
2222 CCCC 调用 Matlab
Matlab
Matlab
Matlab
Matlab
1111)DLLDLLDLLDLL 方法(Matlab
Matlab
Matlab 生成)
/////////////////////////////////////////////////////////////////
function out = add(x,y)
out = x + y;
将M文件编译成DLL:
mcc -W lib:add -T link:lib add.m
用Matlab调用DLL查看函数
loadlibrary('add.dll','add.h');
% 查看add.dll支持的函数接口
libfunctions add -full
/////////////////////////////////////////////////////////////////
将生成的add.h、add.dll、add.lib等拷入项目所在文件夹,并设置VS2010编译器:
a) 项 目 -> 属 性 ->C/C++-> 常 规 -> 附 加 包 含 目 录 :
D:\Softwares\MATLAB\R2009b\extern\include
b) 项 目 -> 属 性 -> 链 接 器 -> 常 规 -> 附 加 库 目 录 :
D:\Softwares\MATLAB\R2009b\extern\lib\win32\microsoft
mclmcrrt.lib
mclmcrrt.lib addaddaddadd.lib.lib.lib.lib (分行没有逗号)
mclmcrrt.lib
c)项目->属性->链接器->常规->输入: mclmcrrt.lib
/////////////////////////////////////////////////////////////////
//此处为静态调用DLL,注意C对格式的要求:变量声明要在函数定义之前
#include "add.h"
mxArray *result = NULL;
mxArray *result_1[1] = {NULL};
mxArray *arguments[2] = {NULL,NULL};
double *output = NULL;
int main( int argc, char * argv[])
{
if ( !mclInitializeApplication(NULL,0))
{
fprintf(stderr, "Could not initialize the application.\n" );
exit(1);
}
if ( !addInitialize())
{
fprintf(stderr, "Could not initialize the library.\n" );
exit(1);
}
arguments[0] = mxCreateDoubleScalar(2.0);
arguments[1] = mxCreateDoubleScalar(3.0);
//result = mxCreateDoubleMatrix(1,1,mxREAL);
//mlxAdd(1, &result, 2, arguments);
mlfAdd(1, &result, arguments[0],arguments[1]);
output = mxGetPr(result);
//mlxAdd(1, result_1, 2, arguments);
mlfAdd(1, result_1, arguments[0],arguments[1]);
output = mxGetPr(result_1[0]);
printf( "result is: %f\n" ,*output);
mxDestroyArray(result);
mxDestroyArray(arguments[0]);
mxDestroyArray(arguments[1]);
addTerminate();
mclTerminateApplication();
return 0;
}
Engine
2222)Engine
Engine
Engine 方法
配置编译器
要在 VC 中成功编译 Matlab 引擎程序,必须包含引擎头文件 engine.h 并引入 Matlab 对应
的库文件 libmx.lib、libmat.lib、libeng.lib。具体的说,打开一个工程后,做如下设置:
a) 项 目 -> 属 性 ->C/C++-> 常 规 -> 附 加 包 含 目 录 :
D:\Softwares\MATLAB\R2009b\extern\include
b) 项 目 -> 属 性 -> 链 接 器 -> 常 规 -> 附 加 库 目 录 :
D:\Softwares\MATLAB\R2009b\extern\lib\win32\microsoft
c)项目->属性->链接器->常规->输入:libmx.lib、libmat.lib、libeng.lib (分行没有逗号),
d) 如此设置后,能够正常编译,但运行时报错说找不到 dll 文件,而 libmx.dll 是存在于
D:\Softwares\MATLAB\R2009b\bin\win32 文件夹中,而在 Win7 系统变量里 Matlab
在安装的时候时只写入了路径 D:\Softwares\MATLAB\R2009b\bin,故系统不会自动到
其子文件中找 dll,从而报错说找不到文件,这也解释了为什么旧版本的 Matlab 无此问题,
因为可能旧版本的 Matlab 中没有将这些 dll 分离放到 win32 子文件夹中。所以只要在系统
变量中包含路径 D:\Softwares\MATLAB\R2009b\bin\win32 即可。
/////////////////////////////////////////////////////////////////
// 实现逆矩阵求取
#include
#include
using namespace std;
#include
包含了
#include
#pragma comment(lib, "libmat.lib")
#pragma comment(lib, "libmx.lib")
#pragma comment(lib, "libeng.lib")
int main()
{
// 使用VC++和Matlab引擎混合编程就必须加这个头文件,它里面
// 定义循环变量
int i = 0, j = 0;
// 状态变量
int nStatus = 0;
// 定义MATLAB引擎指针
Engine *ep = NULL;
// 定义mxArray变量指针
mxArray *A, *B;
// 定义矩阵数组, 注意:VC中矩阵的行列次序与MATLAB正好相反,需要转置
double arData[3][3] = {{7.5,16.3,9.6},{13.2,8.9,12.3},{9.6,5.4,13.7}};
double arResData[3][3];
// 输出原矩阵
cout<<"原矩阵为:"<cout<<"无法打开MATLAB引擎。"<
// 输出计算结果
cout<<"逆矩阵为:"<
// measure.c
#include "collect_one_external.h"
#include
extern double measure_from_device(void);
bool collect_one(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
*(mxGetPr(plhs[0])) = measure_from_device();
}
double measure_from_device(void)
{
static double t = 0.0;
t = t + 0.05;
return sin(t);
}
/////////////////////////////////////////////////////////////////
编译:mcc -m collect.m measure.c
M-Files
2222)MixingMixingMixingMixing M-Files
M-Files andandandand CCCC orororor C++C++C++C++
M-Files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% mrank.m
function r = mrank(n)
r = zeros(n,1);
for k = 1:n
r(k) = rank(magic(k));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% printmatrix.m
function printmatrix(m)
disp(m);
/////////////////////////////////////////////////////////////////
// mrankp.c
#include
#include
#include "libPkg.h"
#include "main_for_lib.h"
int run_main(int ac, char **av)
{
mxArray *N;
mxArray *R = NULL;
int
/* Get any command line parameter. */
n;
/* Matrix containing n. */
/* Result matrix. */
/* Integer parameter from command line. */
if (ac >= 2) {
n = atoi(av[1]);
} else {
n = 12;
}
/* Call the mclInitializeApplication routine. Make sure that the application was
initialized properly by checking the return status. This initialization has to be done
before calling any MATLAB API's or MATLAB Compiler generated shared library
functions. */
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
return -2;
}
/* Call the library intialization routine and make sure that the
* library was initialized properly */
if (!libPkgInitialize())
{
fprintf(stderr,"Could not initialize the library.\n");
return -3;
}
else
{
/* Create a 1-by-1 matrix containing n. */
N = mxCreateDoubleScalar(n);
/* Call mlfMrank, the compiled version of mrank.m. */
mlfMrank(1, &R, N);
/* Print the results. */
mlfPrintmatrix(R);
/* Free the matrices allocated during this computation. */
mxDestroyArray(N);
mxDestroyArray(R);
libPkgTerminate();
}
/* Terminate the library of M-functions */
/* Note that you should call mclTerminate application in the end of
* your application. mclTerminateApplication terminates the entire
* application. */
mclTerminateApplication();
return 0;
}
/////////////////////////////////////////////////////////////////
// main_for_lib.c
#include "main_for_lib.h" /* for the definition of the structure inputs */
int run_main(int ac, const char *av[]);
int main(int ac, const char* av[])