关于vc读取matlab中矩阵的问题。
大概的思路:我是想把vc中的数据写入到matlab中,在matlab中处理,在处理数据后在讲数据从matab中读取出来.
出现错误的地方是,从matlab中获取数据失败。感觉貌似读取出来了,但怎么也显示不出来,matlab初学,很多东西还比较迷糊。求救!!!!!!!!!我都想了一天了也没想出个所以然来。
错误代码:
void CHandle_matlabDlg::OnOK()
{
// TODO: Add extra validation here
Engine *ep;
double num[3];
double num_copy[3];
double *temp;
CString str;
temp=num;
for(int i=0;i<3;i++)
{
num[i]=(i+1)*0.2;
num_copy[i]=1;
}
str.Format("%f =%f =%f",num_copy[0],num_copy[1],num_copy[2]);
AfxMessageBox(str);
mxArray *x_begin=mxCreateDoubleMatrix(1,3,mxREAL); //存储数据 行 列
mxArray *mat=NULL;//保存返回后的结果.
char buffer[256];
if (!(ep=engOpen(NULL)))
{
AfxMessageBox("matlab引擎引导失败!");
exit(1);
}
memcpy(mxGetPr(x_begin),temp,3*sizeof(double));
engPutVariable(ep,"x_n",x_begin); //vc先写到中间变量中在从中间变量写到matlab中
engEvalString(ep,"mat=x_n*2"); //键入字符串. 生成并对x_n进行操作
buffer[255]='\0';
engOutputBuffer(ep,buffer,255);
AfxMessageBox(buffer);
engEvalString(ep,"whos");
//获取变量.并复制到定义的mat中.
if ((mat=engGetVariable(ep,"x_n"))==NULL)//还他们的获取成功了
{
AfxMessageBox("获取失败!");
}
else
{
str.Format("%s",mxGetClassName(mat)); //获取mat类型
AfxMessageBox(str);
}
// mat=engGetArray(ep,"x_n"); // 从matlab中获取一个矩阵.
temp=num_copy;
temp=(double *)mxGetPr(mat); //我个人感觉出错点在这里.
str.Format("%f =%f =%f",num_copy[1],num_copy[1],num_copy[2]);
AfxMessageBox(str);
// CDialog::OnOK();
}