|
一个例子,由此例提请注意的是:
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[3];
- z[0]=z1;
- z[1]=z2;
- z[2]=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=[x1 x2];");
- engEvalString(ep,"d1=[x1 x2 x3];");
- engEvalString(ep,"[y,x,t]=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;
- }
复制代码 |
评分
-
1
查看全部评分
-
|