使用的函数有:命令在 "engine.h" 具体在 /MATLAB701/extern/include
Engine * engOpen(const char *startcmd); // startcmd在windows中一般是NULL, 或者/0; 启动matlab命令窗口
int engClose(Engine *ep); //关闭matlab服务
int engSetVisible(Engine *ep, bool newVal); //设置命令窗口可见与否, 可以修改默认设置
int engGetVisible(Engine * ep, bool * bVal); //获取
mxArray * engGetVariable(Engine *ep, const char * name); //name要获得的变量名称, ep 指针引擎
int engPutVariable(Engine *ep, const char *var_name, const mxArray *ap); 把一个变量ap用特定名称var_name放入matlab工作空间中
int engOutputBuffer(Engine *ep, char * Buffer, int buflen); buffer保存输出的字符串数组,buflen缓冲数组长度
Engine * engOpenSingleUse(const char * startcmd, void * reserved, int * retstatus); //startcmd启动字符串的可执行命令,resered 为NULL 保留以后使用. retstatus 返回状态 . 为单用户使用命令
int engEvalString(Engine *ep, const char *string); //string matlab可执行的命令
参考命令: (加载类库: libeng.lib libmx.lib )
头文件是 #include "engine.h"
Engine *ep;
mxArray *T=NULL, *result = NULL, *mFs = NULL, *mnfft = NULL;
double datax[1024];
char buffer[1024];
for (int j=0; j< 1024; j++)
{
double samt = (double)(1.0/1024);
datax[j] = sin(2.0 * 63.0 * samt * 3.1415926 + 1.15 *3.14159265);
}
double *pPxx , *pFxx;
if (!(ep = engOpen("/0")))
{
fprintf(stderr, "/n Can't start MATLAB engine/n ");
exit(-1);
}
engSetVisible(ep, TRUE);
double Fs[1] = {1024};
double nfft[1] = {1024};
T = mxCreateDoubleMatrix(1, 1024, mxREAL);
mnfft = mxCreateDoubleMatrix(1, 1, mxREAL);
mFs = mxCreateDoubleMatrix(1, 1,mxREAL);
//mxSetName(T, "T");
memcpy((char*)mxGetPr(T), (char*)datax, 1024 *sizeof(double) );
memcpy((char*)mxGetPr(mnfft), (char*)nfft, sizeof(double));
memcpy((char*)mxGetPr(mFs), (char*)Fs, 1*sizeof(double));
engPutVariable(ep,"T", T);
engPutVariable(ep, "mnfft",mnfft);
engPutVariable(ep,"mFs",mFs);
engEvalString(ep, "[pxx,fo] = psd(T, mnfft, mFs);");
engOutputBuffer(ep,buffer, 512);
//result = engGetVariable(ep,"pxx");
//pPxx = mxGetPr(result);
//result = engGetVariable(ep, "fo");
//pFxx =mxGetPr(result);
engEvalString(ep, "plot(fo, 10*log10(pxx))");
engEvalString(ep, "title('功率普分析');");
engEvalString(ep,"xlabel('Hz');");
engEvalString(ep,"ylabel('db');");
//mxDestroyArray(T); //释放内存
//mxDestroyArray(mFs);
//mxDestroyArray(mnfft);
//mxDestroyArray(result);
//engEvalString(ep,"close;");
//engClose(ep);