浅谈VC6.0和Matlab混合编程之Matlab引擎

 from: http://peng-jun.blog.163.com/blog/static/21562814200931585325470/

 

一、配置Matlab环境

1、需要将Matlab的bin目录添加到系统的环境变量path中,比如我的Matlab安装在:E:/Program Files/MATLAB/R2008a,那么我就需要将路径:E:/Program Files/MATLAB/R2008a/bin/win32,添加到系统的环境变量path中,如下:

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

2、开始-》运行,如下:

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

之后,会出现一个Matlab Command Window,则说明已经配置成功了。

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

二、配置VC6.0环境

1、在VC的目录里面添加Matlab目录下external下面的包含和库文件,如下:

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

2、在工程的设置属性中,添加静态链接库:libeng.lib。如下:

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

这一步,需要新建工程之后设置。

三、编写程序

界面如下:

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

改程序为基于对话框的程序。其*Dlg.h的内容如下:

// MatlabEngineDlg.h : header file

//

 

#if !defined(AFX_MATLABENGINEDLG_H__882D2626_4CF6_4288_A3A9_49620185528D__INCLUDED_)

#define AFX_MATLABENGINEDLG_H__882D2626_4CF6_4288_A3A9_49620185528D__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#include "engine.h"//添加Matlab引擎需要的头文件

#define MAX_OUTPUT 2000  //定义输入缓冲区的最大字符数

 

/

// CMatlabEngineDlg dialog

 

class CMatlabEngineDlg : public CDialog

{

// Construction

public:

    CMatlabEngineDlg(CWnd* pParent = NULL);   // standard constructor

    ~CMatlabEngineDlg();//添加一个析购函数

 

public:

    Engine *m_ep;//指向Matlab引擎的一个指针

    char m_outputbuff[MAX_OUTPUT];//用来保存Matlab引擎输出的字符数组

 

// Dialog Data

    //{{AFX_DATA(CMatlabEngineDlg)

    enum { IDD = IDD_MATLABENGINE_DIALOG };

    CEdit  m_output;//与输入区域的Edit关联的一个控件变量

    CString    m_cmd;//与执行命令的Edit关联的一个Cstring变量

    //}}AFX_DATA

 

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CMatlabEngineDlg)

    protected:

    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

    //}}AFX_VIRTUAL

 

// Implementation

protected:

    HICON m_hIcon;

 

    // Generated message map functions

    //{{AFX_MSG(CMatlabEngineDlg)

    virtual BOOL OnInitDialog();

    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

    afx_msg void OnPaint();

    afx_msg HCURSOR OnQueryDragIcon();

    afx_msg void OnBtnEvalstring();

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

 

#endif // !defined(AFX_MATLABENGINEDLG_H__882D2626_4CF6_4288_A3A9_49620185528D__INCLUDED_)

而*Dlg.cpp的程序如下:

// MatlabEngineDlg.cpp : implementation file

//

 

#include "stdafx.h"

#include "MatlabEngine.h"

#include "MatlabEngineDlg.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

/

// CAboutDlg dialog used for App About

 

class CAboutDlg : public CDialog

{

public:

    CAboutDlg();

 

// Dialog Data

    //{{AFX_DATA(CAboutDlg)

    enum { IDD = IDD_ABOUTBOX };

    //}}AFX_DATA

 

    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CAboutDlg)

    protected:

    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    //}}AFX_VIRTUAL

 

// Implementation

protected:

    //{{AFX_MSG(CAboutDlg)

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};

 

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{

    //{{AFX_DATA_INIT(CAboutDlg)

    //}}AFX_DATA_INIT

}

 

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CAboutDlg)

    //}}AFX_DATA_MAP

}

 

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

    //{{AFX_MSG_MAP(CAboutDlg)

       // No message handlers

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

/

// CMatlabEngineDlg dialog

 

CMatlabEngineDlg::CMatlabEngineDlg(CWnd* pParent /*=NULL*/)

    : CDialog(CMatlabEngineDlg::IDD, pParent)

{

    //{{AFX_DATA_INIT(CMatlabEngineDlg)

    m_cmd = _T("");

    //}}AFX_DATA_INIT

    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32

    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

//为成员变量初始化

    m_ep=NULL;

    memset(m_outputbuff,0,MAX_OUTPUT);

}

 

CMatlabEngineDlg::~CMatlabEngineDlg()

{

    if(m_ep!=NULL)

    {

       engClose(m_ep);//关闭Matlab引擎

    }

    m_ep=NULL;

}

 

void CMatlabEngineDlg::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CMatlabEngineDlg)

    DDX_Control(pDX, IDC_EDT_OUTPUT, m_output);

    DDX_Text(pDX, IDC_EDT_CMD, m_cmd);

    //}}AFX_DATA_MAP

}

 

BEGIN_MESSAGE_MAP(CMatlabEngineDlg, CDialog)

    //{{AFX_MSG_MAP(CMatlabEngineDlg)

    ON_WM_SYSCOMMAND()

    ON_WM_PAINT()

    ON_WM_QUERYDRAGICON()

    ON_BN_CLICKED(IDC_BTN_EVALSTRING, OnBtnEvalstring)

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

/

// CMatlabEngineDlg message handlers

 

BOOL CMatlabEngineDlg::OnInitDialog()

{

    CDialog::OnInitDialog();

 

    // Add "About..." menu item to system menu.

 

    // IDM_ABOUTBOX must be in the system command range.

    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

    ASSERT(IDM_ABOUTBOX < 0xF000);

 

    CMenu* pSysMenu = GetSystemMenu(FALSE);

    if (pSysMenu != NULL)

    {

       CString strAboutMenu;

       strAboutMenu.LoadString(IDS_ABOUTBOX);

       if (!strAboutMenu.IsEmpty())

       {

           pSysMenu->AppendMenu(MF_SEPARATOR);

           pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

       }

    }

 

    // Set the icon for this dialog.  The framework does this automatically

    //  when the application's main window is not a dialog

    SetIcon(m_hIcon, TRUE);         // Set big icon

    SetIcon(m_hIcon, FALSE);    // Set small icon

   

    // TODO: Add extra initialization here

    m_ep=engOpen(NULL);//打开Matlab引擎

//将Matlab引擎的输出缓冲区设置为成员变量m_outputbuff

    engOutputBuffer(m_ep,m_outputbuff,MAX_OUTPUT);

engSetVisible(m_ep,false);//设置Matlab引擎窗口不可见

   

    return TRUE;  // return TRUE  unless you set the focus to a control

}

 

void CMatlabEngineDlg::OnSysCommand(UINT nID, LPARAM lParam)

{

    if ((nID & 0xFFF0) == IDM_ABOUTBOX)

    {

       CAboutDlg dlgAbout;

       dlgAbout.DoModal();

    }

    else

    {

       CDialog::OnSysCommand(nID, lParam);

    }

}

 

// If you add a minimize button to your dialog, you will need the code below

//  to draw the icon.  For MFC applications using the document/view model,

//  this is automatically done for you by the framework.

 

void CMatlabEngineDlg::OnPaint()

{

    if (IsIconic())

    {

       CPaintDC dc(this); // device context for painting

 

       SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

 

       // Center icon in client rectangle

       int cxIcon = GetSystemMetrics(SM_CXICON);

       int cyIcon = GetSystemMetrics(SM_CYICON);

       CRect rect;

       GetClientRect(&rect);

       int x = (rect.Width() - cxIcon + 1) / 2;

       int y = (rect.Height() - cyIcon + 1) / 2;

 

       // Draw the icon

       dc.DrawIcon(x, y, m_hIcon);

    }

    else

    {

       CDialog::OnPaint();

    }

}

 

// The system calls this to obtain the cursor to display while the user drags

//  the minimized window.

HCURSOR CMatlabEngineDlg::OnQueryDragIcon()

{

    return (HCURSOR) m_hIcon;

}

 

void CMatlabEngineDlg::OnBtnEvalstring()

{

    // TODO: Add your control notification handler code here

    if(m_ep!=NULL)

    {

       m_output.SetWindowText("");//清空输出区域

       UpdateData(TRUE);//更新数据

       LPSTR cmd=m_cmd.GetBuffer(m_cmd.GetLength());//将要执行的命令转换为字符指针类型

       engEvalString(m_ep,(const char*)cmd);//在Matlab引擎中执行字符串cmd

       m_output.SetWindowText(m_outputbuff);//设置输出区域的内容

    }

    else

    {

       AfxMessageBox("打开Matlab引擎失败!");

       return;

    }

}

运行效果如下:

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

浅谈VC6.0和Matlab混合编程之Matlab引擎 - pJ27 - pJ27

有关更多的Matlab引擎函数,可以查看engine.h中的函数声明。

点击此处下载本例子:MatlabEngine.7z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值