调用matlab引擎通过txt绘图
本帖最后由 sunminmin 于 2011-5-10 08:47 编辑在matlab的commond窗口中键入下面的代码能得到map.txt对应的图形:(注:map.txt中每一行为一个点的坐标(x,y,z)。)
M = dlmread('map.txt'); % 读取map.txt文件中的数据
x = M(:,1); % 取第一列
y = M(:,2); % 取第二列
z = M(:,3); % 取第三列
x = reshape(x,65,65); % 重构为size=65*65的方格
y = reshape(y,65,65); % 重构为size=65*65的方格
z = reshape(z,65,65); % 重构为size=65*65的方格
surf(x,y,z) % 绘图
shading flat %各小曲面之间不要网格
xlabel('X(单位:千米)')
ylabel('Y(单位:千米)')
zlabel('Z(单位:千米)')
但是在VC++中建立MFC AppWizard基于基本对话框的工程TxtToMap,调用matlab引擎绘不出map.txt对应的图形:
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);
}
}
void CTxtToMapDlg::OnButtonTxtToMap()
{
// TODO: Add your control notification handler code here
Engine *ep;
if (!(ep = engOpen(NULL))) //打开Matlab引擎
{
MessageBox ("Can't start MATLAB engine", MB_OK);
exit(-1);
}
int N = 4225;
mxArray *M = mxCreateDoubleMatrix(N,3,mxREAL);
mxArray *x = mxCreateDoubleMatrix(1,N,mxREAL);
mxArray *y = mxCreateDoubleMatrix(1,N,mxREAL);
mxArray *z = mxCreateDoubleMatrix(1,N,mxREAL);
engPutVariable(ep,"M",M);
engPutVariable(ep,"x",x);
engPutVariable(ep,"y",y);
engPutVariable(ep,"z",z);
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,"x = reshape(x,65,65)"); //重构为size=65*65的方格
engEvalString(ep,"y = reshape(y,65,65)"); //重构为size=65*65的方格
engEvalString(ep,"z = reshape(z,65,65)"); //重构为size=65*65的方格
engEvalString(ep,"surf(x,y,z)"); //绘图
}
我刚开始学vc++与matlab混合编程调用matlab引擎,我自己也知道上面void CTxtToMapDlg::OnButtonTxtToMap() 中的代码写的有错,请问如何修改才能得到map.txt对应的图形?
本帖最后由 sunminmin 于 2011-5-11 16:33 编辑
回复 1 # sunminmin 的帖子
如何调用matlab引擎把map.txt load进matlab,让matlab绘出对应的三维图?关键是这个数据量太大,怎样拟合才能通过matlab引擎把图绘出来?
上面的void CTxtToMapDlg::OnButtonTxtToMap()怎么修改,那样写错在哪里?
能这样用吗? engEvalString(ep,"M = dlmread('m_FilePath')"); //m_FilePath保存map.txt的路径,读取map.txt文件中的数据
还是靠自己解决了。{:{46}:}
Engine *ep;
if (!(ep = engOpen(NULL))) //打开Matlab引擎
{
MessageBox ("Can't start MATLAB engine", MB_OK);
exit(-1);
}
int N = 4225; //txt文件中有4225行数据
mxArray *M = mxCreateDoubleMatrix(N,3,mxREAL);
mxArray *x = mxCreateDoubleMatrix(1,N,mxREAL);
mxArray *y = mxCreateDoubleMatrix(1,N,mxREAL);
mxArray *z = mxCreateDoubleMatrix(1,N,mxREAL);
mxArray *xx = mxCreateDoubleMatrix(65,65,mxREAL);
mxArray *yy = mxCreateDoubleMatrix(65,65,mxREAL);
mxArray *zz = mxCreateDoubleMatrix(65,65,mxREAL);
engPutVariable(ep,"M",M);
engPutVariable(ep,"x",x);
engPutVariable(ep,"y",y);
engPutVariable(ep,"z",z);
engPutVariable(ep,"xx",x);
engPutVariable(ep,"yy",y);
engPutVariable(ep,"zz",z);
CString s1("M = load('");
CString s2("')");
CString s = s1+m_FilePath+s2;
char * p = (LPSTR)(LPCTSTR)s;
engEvalString(ep,p);
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,"mesh(xx,yy,zz)"); //绘图
engEvalString(ep,"xlabel('X(单位:千米)')");
engEvalString(ep,"ylabel('Y(单位:千米)')");
engEvalString(ep,"zlabel('Z(单位:百米)')");
解决问题之后回来分享经验
在那个调用引擎画图的那个消息响应函数里有错,即engEvalString(ep,"M = load('m_FilePath')");有错。
就是调用m_filename变量的时候,传的是“M = load(m_filename.txt)"是字符串啊,没办法调出m_filename的值来
M = load('m_FilePath')"这句话再matlab里是不能够知道m_FilePath是什么的,得把几个字符串拼接起来
M = load('+ m_FilePath+ ')" 把这3段字符串拼接然后传入matlab 。
现在的问题就是
比如
m_FilePath = "C:\New folder\my.txt"
engEvalString(ep,"M = load('m_FilePath')");这句话运行后相当于在matlab里运行这句话M = load('m_FilePath'),
但是实际上应该传入这句话M = load('C:\New folder\my.txt')
用下面的语句
CString s1("M = load('");
CString s2("')");
Ctring s = s1+m_FilePath+s2;
char * p = (LPSTR)(LPCTSTR)s;
engEvalString(ep,p);
替换engEvalString(ep,"M = load('+m_FilePath+')");
这句
问题就解决了!
页:
[1]