How To Connect Matlab and Delphi 7
How To Connect Matlab and Delphi 7
How To Connect Matlab and Delphi 7
By Corzo
This article describes the process of connecting a delphi code with Matlab 7 automaton server
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);
http://www.codeproject.com/useritems/MatlabDelphiCOM.asp
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)');
About Corzo
Civil Engineer ECI Msc. Hydroinformatics UNESCO-IHE Delft(Holland) Actually Research Fellow
http://www.codeproject.com/useritems/MatlabDelphiCOM.asp