A class wrapper for Matlab(c) ActiveX Controlhttp://vccode.com/vcfile/file/20020917205954_matlabengine.rar使用说明: 原作者Jonathan de Halleux ,代码大小61k。 完成时间:16 Sep 2002 环境:VC7, VC6, XP, W2K, Win9X下面是翻译的作者的说明:介绍Matlab(c)是一个著名的软件,包括一些烈的复杂而功能强大的工具。Matlab(c)有自己的设计语言,使用它你可以开发自己的程序。然而,有事我们想要一个C\C++程序来运行它。实际上,尽管Matlab(c)使用C的API函数,但是使用起来是很复杂的:连接问题、编译指定的标记还有一些莫名其妙的bug。另一个问题是要使用Active控件的COM接口。使用这个接口有困难的,因为它是COM,要使用一些难解的代码...CMatlabEngine类解决了这个问题。它隐藏了所有关于COM的晦涩的代码(比如得到CLSID,得到IDispatch等等)。使你能够集中于主要工作的设计:使用Matlab(c)。这篇文章描述了这个类的主要功能,更过细节请看工程中的Doxygen文档。初始化引擎所有的初始化代码都在CMatlabEngine的构造函数中。你可以通过调用IsInited函数得到检查server是否初始化成功: CMatlabEngine mt; if (!mt.IsInited()) // could not initialize matlab engine, do something about it引擎的主要方法matlab引擎包括三个主要方法:Execute, PutMatrix, GetMatrix 执行Matlab(c)的代码:调用Execute( LPCTSTR szMatlabCode ): // this code will show the classic Matlab(c) demo mt.Execute(_T("surf(peaks)")); mt.Execute(_T("title('demo')"));为matlab设置队列UINT nRows=10; // number of rows UINT nCols=2; // number of columns vector v( nRows * nCols ); // filling up v ... // sending v to Matlab(c) with name "Mv" mt.PutMatrix( _T("M"), v, nRows, nCols); // M is now a matrix in the Matlab(c) workspace从matlab返回队列mt.Execute(_T("v=[1 2 3; 4 5 6]';")); vector vReal(6); // vReal must have the correct size... mt.GetMatrix(_T("v"), 3, 2, vReal); 工作空间你可以使用SetWorkspace改变工作空间。默认情况下,这个工作空间是"base"。如果你想声明全局变量,那么使用"global"工作空间。映射matlab窗口状态最小化命令窗口, mt.MinimizeWindow();最大化命令窗口, mt.MaximizeWindow();显示与隐藏命令窗口, mt.Show( true /* true to make it visible, false to hide it*/);得到命令窗口的状态, if(mt.IsVisivle()) // Matlab is server is visible...Quit the command window, mt.Quit(); 已知的bug GetMatrix不能正常工作,如果谁能修复,请通知我。 发布者: soarlove
环境:VC7, VC6, XP, W2K, Win9X下面是翻译的作者的说明:介绍Matlab(c)是一个著名的软件,包括一些烈的复杂而功能强大的工具。Matlab(c)有自己的设计语言,使用它你可以开发自己的程序。然而,有事我们想要一个C\C++程序来运行它。实际上,尽管Matlab(c)使用C的API函数,但是使用起来是很复杂的:连接问题、编译指定的标记还有一些莫名其妙的bug。另一个问题是要使用Active控件的COM接口。使用这个接口有困难的,因为它是COM,要使用一些难解的代码...CMatlabEngine类解决了这个问题。它隐藏了所有关于COM的晦涩的代码(比如得到CLSID,得到IDispatch等等)。使你能够集中于主要工作的设计:使用Matlab(c)。这篇文章描述了这个类的主要功能,更过细节请看工程中的Doxygen文档。初始化引擎所有的初始化代码都在CMatlabEngine的构造函数中。你可以通过调用IsInited函数得到检查server是否初始化成功:
CMatlabEngine mt;
if (!mt.IsInited())
// could not initialize matlab engine, do something about it引擎的主要方法matlab引擎包括三个主要方法:Execute, PutMatrix, GetMatrix 执行Matlab(c)的代码:调用Execute( LPCTSTR szMatlabCode ):
// this code will show the classic Matlab(c) demo
mt.Execute(_T("surf(peaks)"));
mt.Execute(_T("title('demo')"));为matlab设置队列UINT nRows=10; // number of rows
UINT nCols=2; // number of columns
vector v( nRows * nCols );
// filling up v
...
// sending v to Matlab(c) with name "Mv"
mt.PutMatrix( _T("M"), v, nRows, nCols);
// M is now a matrix in the Matlab(c) workspace从matlab返回队列mt.Execute(_T("v=[1 2 3; 4 5 6]';"));
vector vReal(6); // vReal must have the correct size...
mt.GetMatrix(_T("v"), 3, 2, vReal); 工作空间你可以使用SetWorkspace改变工作空间。默认情况下,这个工作空间是"base"。如果你想声明全局变量,那么使用"global"工作空间。映射matlab窗口状态最小化命令窗口, mt.MinimizeWindow();最大化命令窗口, mt.MaximizeWindow();显示与隐藏命令窗口, mt.Show( true /* true to make it visible, false to hide it*/);得到命令窗口的状态,
if(mt.IsVisivle())
// Matlab is server is visible...Quit the command window, mt.Quit();
已知的bug
GetMatrix不能正常工作,如果谁能修复,请通知我。
发布者: soarlove