作者在 2014-05-16 15:12:55 发布以下内容
一、Matlab中生成共享库
1、打开matlab编译器命令行
mcc -setup (选择对应的visual stdio版本)
2、在matlab中新建Deployment Project,然后添加文件,生成.h、.lib、.dll
二、在C项目中调用相应的函数
1、工程中,项目->属性->配置属性->VC++目录中包含目录和库目录分别改成matlab安装目录下 extern\include和extern\lib\win32\microsoft.
2、把头文件和库文件拷贝到VC项目目录下面,并把头文件添加到项目
3、在项目中添加库文件#pragma comment(lib,"xxx.lib")
void fir(float *fB,UINT len,float fc,float fs)
{
if(!mclInitializeApplication(NULL, 0)){
printf("mclInitializeApplication is bad\n");
return;
}
else
printf("mclInitializeApplication is ok\n");
//****************初始化用户的matlab函数dll库*************************//
if(!firInitialize()){
printf("firInitialize is bad\n");
return;
}
else
printf("firInitialize is ok\n");
mxArray *n, *wn;
mxArray *out=NULL,*out_A=NULL;
double t_wn=2*fc/fs-(int)(2*fc/fs);
double *B=(double *)malloc(sizeof(double)*len);
n = mxCreateDoubleScalar(len-1);
wn = mxCreateDoubleScalar(t_wn);
mlfFir(2, &out,&out_A, n, wn);
memcpy(B, mxGetPr(out), len*sizeof(double));
for(int i=0;i<len;i++)
fB[i]=(float)B[i];
free(B);
mxDestroyArray(out);
mxDestroyArray(out_A);
firTerminate();
mxDestroyArray(n);
mxDestroyArray(wn);
mclTerminateApplication();
}