E02610229 发表于 2006-9-2 10:54

VC调用matlab,实参怎么传递?

我用matlab编写了computfft.m文件,然后将它生成了.dll文件,想用VC来调用,请问怎么给它赋实参.在
function =computfft(source,sample,slength)中,source和sample是一个二唯矩阵,slength是int型,然后返回的参数offset,flag是double

AaronSpark 发表于 2006-9-3 08:25

一个例子,由此例提请注意的是:
1. matlab中的数组下标从1开始;
2. maltab中的数据及时与VC中同名也并非同一个存储空间;
3. 学会应用各种传入传出函数




#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
int transs(double z1,double z2,double z3) ;
#define BUFSIZE 256
void main()
{
double z1=2;
double z2=3;
double z3=7;
printf("z1=%f,z2=%f,z3=%f\n",z1,z2,z3);
system("pause");
transs(z1,z2,z3);
}

int transs(double z1,double z2,double z3)
{
Engine *ep;

mxArray *z_ptr,*result=NULL;
mxArray *z4_ptr;
double *z4;
double z;
z=z1;
z=z2;
z=z3;
if(!(ep=engOpen("{followups.content}")))
{
fprintf(stderr,"\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
z_ptr=mxCreateDoubleMatrix(1,3,mxREAL);
z4_ptr=mxCreateDoubleMatrix(1,2,mxREAL);
memcpy((void *)mxGetPr(z_ptr),(void *)z,sizeof(z));
printf("z_ptr=%f\n",z_ptr);
engPutVariable(ep,"z_ptr",z_ptr);
printf("z_ptr=%f\n",z_ptr);
engEvalString(ep,"x1=z_ptr(1,1)");
engEvalString(ep,"x2=z_ptr(1,2);");
engEvalString(ep,"x3=z_ptr(1,3)");
engEvalString(ep,"n1=;");
engEvalString(ep,"d1=;");
engEvalString(ep,"=step(n1,d1);");
engEvalString(ep,"z4_ptr=max(y);");
z4_ptr=engGetVariable(ep,"z4_ptr");
z4=mxGetPr(z4_ptr);
printf("z4=%f\n",*z4);
mxDestroyArray(z_ptr);
mxDestroyArray(z4_ptr);
engClose(ep);
return 1;
}

wahaha_2002 发表于 2006-9-6 20:29

好,谢谢!
页: [1]
查看完整版本: VC调用matlab,实参怎么传递?