希望增加关于Delphi Com编程方面的讨论 ( 积分: 50 )

  • 主题发起人 主题发起人 shijiesun
  • 开始时间 开始时间
S

shijiesun

Unregistered / Unconfirmed
GUEST, unregistred user!
我这里有珍藏的Delphi连接Matlab计算引擎的帖子,先奉献出来:
Delphi 7 is a complete developing language, and Matlab 7 have so many engineering toolbox and ploting tools. So if we can combine them, it will be a powerfull graphical interface plus the best mathematical tool. This process is very simple, however, the first time i tried to find information about it was very difficult.
The matlab automation server allows to execute any command as if you where inside the matlab enviroment but from any other enviroment, even office. In this case the example uses DELPHI. The code i am presenting here is to load data from a filename given from delphi and execute a plot function (Time series). But as you will see is just an example and can be used to execute any other function.

The matlab by default doesnt register the libraries for this comunication, so you will need to go to the command line, in the folder <MATLAB7/BIN/WIN32> and type mwregsvr mwcomutil and mwregsvr mwcommgr.

Now you are ready to call matlab from DELPHI.
First open a project. In side the form create two buttons, in this case i used Bitbutton. So you can se in the procedure Form1.BitBtn. one of the buttons is to connect with matlab, the other one is just to close the form.

Delphi requires some libraries to comunicate with COM objects. In this case you need to add to the standard ones ComCtrls, ComObj, which are the ones that allow the communication

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, Buttons, ExtCtrls, ComCtrls,ComObj;

Then inside one of the buttons, create the OLE object, this is done by instruction CreateOleObject. After this you just need to apply anyone of the function available for the communication. Which are not so much in principle (TABLE 1). But are enough. You can see that in this first button the option visible is set to 1, these is because i wanted to use matlab while delphi was running, so you can leave it in 0. This process delays a little depending on the processor you are ussing, but after it loads is much faster.

<CODE>procedure TForm1.BitBtn2Click(Sender: TObject);
var name:string;
begin
MatApp:=CreateOleObject('matlab.application');
MatApp.visible:=1;

end;
To send something just add the following line, in whcih the first parameter of the method PutWorkspaceData is the matlab variable that will be created, the second one is the type of variable (can be added to the base space or to the global space), and the last one is the name in of the variable in DELPHI.

name:='MyFileNameWithNumericalData.txt';
MatApp.PutWorkspaceData('File_Arff','base', name);
Finally, to execute use the method Execute, the only parameter needed is the string with the command required.

MatApp.Execute('A=load('File_Arff');');
MatApp.Execute('plot(A)');
 
我这里有珍藏的Delphi连接Matlab计算引擎的帖子,先奉献出来:
Delphi 7 is a complete developing language, and Matlab 7 have so many engineering toolbox and ploting tools. So if we can combine them, it will be a powerfull graphical interface plus the best mathematical tool. This process is very simple, however, the first time i tried to find information about it was very difficult.
The matlab automation server allows to execute any command as if you where inside the matlab enviroment but from any other enviroment, even office. In this case the example uses DELPHI. The code i am presenting here is to load data from a filename given from delphi and execute a plot function (Time series). But as you will see is just an example and can be used to execute any other function.

The matlab by default doesnt register the libraries for this comunication, so you will need to go to the command line, in the folder <MATLAB7/BIN/WIN32> and type mwregsvr mwcomutil and mwregsvr mwcommgr.

Now you are ready to call matlab from DELPHI.
First open a project. In side the form create two buttons, in this case i used Bitbutton. So you can se in the procedure Form1.BitBtn. one of the buttons is to connect with matlab, the other one is just to close the form.

Delphi requires some libraries to comunicate with COM objects. In this case you need to add to the standard ones ComCtrls, ComObj, which are the ones that allow the communication

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, Buttons, ExtCtrls, ComCtrls,ComObj;

Then inside one of the buttons, create the OLE object, this is done by instruction CreateOleObject. After this you just need to apply anyone of the function available for the communication. Which are not so much in principle (TABLE 1). But are enough. You can see that in this first button the option visible is set to 1, these is because i wanted to use matlab while delphi was running, so you can leave it in 0. This process delays a little depending on the processor you are ussing, but after it loads is much faster.

<CODE>procedure TForm1.BitBtn2Click(Sender: TObject);
var name:string;
begin
MatApp:=CreateOleObject('matlab.application');
MatApp.visible:=1;

end;
To send something just add the following line, in whcih the first parameter of the method PutWorkspaceData is the matlab variable that will be created, the second one is the type of variable (can be added to the base space or to the global space), and the last one is the name in of the variable in DELPHI.

name:='MyFileNameWithNumericalData.txt';
MatApp.PutWorkspaceData('File_Arff','base', name);
Finally, to execute use the method Execute, the only parameter needed is the string with the command required.

MatApp.Execute('A=load('File_Arff');');
MatApp.Execute('plot(A)');
 
以后要多多交流
 
好,帮顶



--------签名档---------------------------

比肩国内顶尖源码下载站点 -> 源码我爱你

http://www.source520.com
http://www.source520.net

80G源码电子书免费免注册下载,大量精辟技术文档库随时更新
******************************************************************
附:为了站点持续发展,现有本站近年来收藏的大量大型商业源码低价出售,
详情请进入以下链接查看:
http://www.source520.com/building_delphi.htm

浏览商业代码请从如下URL进入查看实物:
1.商业源码库1: ftp://source520see3:browse@61.152.199.245/
2.商业源码库2: ftp://source520see2:browse@61.152.199.245/
 
后退
顶部