您的位置:首页 > 编程语言 > MATLAB

Matlab VC 联合编程 控制台以及MFC中实现(五)

2010-11-12 18:24 459 查看
第三个程序:

采用多线程的MFC调用matlab函数:

同样的加入相关文件

#include "stdafx.h"
#include "matlabPlotMFC2.h"
#include "matlabPlotMFC2Dlg.h"
#include "matlab_header.h"
#pragma comment(lib, "plot.lib")
#include "plot.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


在OnInitDialog中加入

// TODO: 在此添加额外的初始化代码
if( !mclInitializeApplication(NULL,0) )
{
//std::cerr<<"could not initialize the application"<
//return -2;
return 0;
}
if (!plotInitialize())
{
//std::cerr<<"could not initialize plot/n";
//return -3;
return 0;
}


线程函数为:

UINT plotThreadFunc(LPVOID lpParam)
{
mwArray fileName("2D_Scan_data_2010_10_19_10_10.txt");
Scan_Data_Display(fileName);
mclWaitForFiguresToDie(NULL);
return 0;
}


消息响应函数为:

void CmatlabPlotMFC2Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
AfxBeginThread((AFX_THREADPROC)plotThreadFunc, this,THREAD_PRIORITY_NORMAL, 0,0,NULL);
}
void CmatlabPlotMFC2Dlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
plotTerminate();
mclTerminateApplication();
OnCancel();
}


采用多线程的程序就不会出现死掉的情况了,同时可以多次点击确定按钮多次运行matlab接口函数,程序运行效果如图



比较一下第二和第三个程序的运行时间,不难发现第三个程序更为合理.

欢迎各位与我讨论 chen0510566@163.com

所有源程序在这里可以下到http://download.csdn.net/source/2827645
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: