注册 登录
编程论坛 VC++/MFC

vc++调用matlab引擎的API函数出错

sunminmin 发布于 2011-05-12 13:17, 604 次点击
程序代码:
帮忙看看void CTxtToMapDlg::OnButtonTxtToMap()为何不正确?


个人觉得  engEvalString(ep,"M = dlmread(m_FilePath','',');"); //m_FilePath保存map.txt的路径,读取map.txt文件数据  这里有错??   但不确定。



 
//用来获取文件的路径,并保存进m_FilePath(注:m_FilePath为编辑框的标号,编辑框用来显示路径名用)
void CTxtToMapDlg::OnButtonBrowsing()
{
    // TODO: Add your control notification handler code here
    char szFilters[] = "MyType Files (*.txt)|*.txt|All Files (*.*)|*.*||";
   
    CFileDialog fileDlg (TRUE, "txt", "*.txt",OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
   
    // Display the file dialog. When user clicks OK, fileDlg.DoModal()
   
// returns IDOK.
    if( fileDlg.DoModal()==IDOK )
    {
        CString pathName = fileDlg.GetPathName();
      
        m_FilePath = pathName;     //m_FilePath里保存txt文件的路径
      
        UpdateData(FALSE);  
    }
}




//处理txt中数据绘图
void CTxtToMapDlg::OnButtonTxtToMap()
{
    // TODO: Add your control notification handler code here
   
   
    Engine *ep;              
   
    if (!(ep = engOpen(NULL))) //打开Matlab引擎
      
    {            
      
        AfxMessageBox ("Can't start MATLAB engine", MB_OK);
      
        exit(-1);
    }
   
    int MCOUNT = 4225;               //map.txt中有4225行,即4225个(x,y,z)坐标
    int N = 65;                     //x,y值为1,2,3,4,.......,65
   
    mxArray *M = mxCreateDoubleMatrix(MCOUNT,3,mxREAL);  //MCOUNT*3矩阵用来放txt中的三列数据
  
//mxCreateDoubleMatrix(m, n, mxREAL); 创建m*n的矩阵
   
    mxArray *x = mxCreateDoubleMatrix(MCOUNT,1,mxREAL);//定义存放txt第一列即x坐标的矩阵
    mxArray *y = mxCreateDoubleMatrix(MCOUNT,1,mxREAL);
    mxArray *z = mxCreateDoubleMatrix(MCOUNT,1,mxREAL);
   
    mxArray *xx = mxCreateDoubleMatrix(N,N,mxREAL);
    mxArray *yy = mxCreateDoubleMatrix(N,N,mxREAL);
    mxArray *zz = mxCreateDoubleMatrix(N,N,mxREAL);
   
    engPutVariable(ep,"M",M);  //把变量M写进matlab的工作空间
    engPutVariable(ep,"x",x);
    engPutVariable(ep,"y",y);
    engPutVariable(ep,"z",z);

    engPutVariable(ep,"xx",xx);
    engPutVariable(ep,"yy",yy);
    engPutVariable(ep,"zz",zz);
   
    engEvalString(ep,"M = dlmread(m_FilePath','',');"); //m_FilePath保存map.txt的路径,读取map.txt文件数据
   
    engEvalString(ep,"x = M(:,1)");             //取第一列
    engEvalString(ep,"y = M(:,2)");             // 取第二列
    engEvalString(ep,"z = M(:,3)");             // 取第三列
    engEvalString(ep,"xx = reshape(x,65,65)");   //重构为size=65*65的方格
    engEvalString(ep,"yy = reshape(y,65,65)");   //重构为size=65*65的方格
    engEvalString(ep,"zz = reshape(z,65,65)");   //重构为size=65*65的方格
    engEvalString(ep,"surf(xx,yy,zz)");            //绘图
      
}




map.txt中数据格式,每一行为一个(x,y,z)坐标:
  1.0000000e+000  1.0000000e+000  2.0000000e+000
  1.0000000e+000  2.0000000e+000  2.4651125e+000
  1.0000000e+000  3.0000000e+000  2.4267769e+000
   。。。




[ 本帖最后由 sunminmin 于 2011-5-12 13:36 编辑 ]
0 回复
1