Execute Matlab Command From Delphi Application

11 06 2010

Matlab merupakan salah satu perangkat lunak yang cukup handal untuk perhitungan komputasi numerik. Matlab memungkinkan manipulasi matriks, mem-plot fungsi dan data, implementasi algoritma, pembuatan antarmuka pengguna, dan antarmuka dengan program dalam bahasa lainnya. Tulisan kali ini akan mengulas sedikit tentang antarmuka pemrograman matlab dan pemrograman delphi atau istilah lainnya, menjalankan perintah matlab pada aplikasi delphi yang kita buat. Untuk lebih jelasnya, silahkan simak yang satu ini………… ^_^

Introduction

MATLAB functions and properties enable an Automation controller to manipulate data in the MATLAB workspace. MATLAB can be both a controller and a server.

This section explains how to call functions in the MATLAB Automation server and how to use properties that affect the server.

For a complete list of these functions, see Component Object Model and ActiveX in the MATLAB Function Reference documentation.

(http://www.mathworks.com)

Executing Commands in the MATLAB Server

The client program can execute commands in the MATLAB server using these functions.

Function Description
Execute Execute MATLAB command in server
Feval Evaluate MATLAB command in server

Using Execute

Use the Execute function when you want the MATLAB server to execute a command that can be expressed in a single string. For example:

h = actxserver('matlab.application');
h.PutWorkspaceData('A', 'base', rand(6))
h.Execute('A(4:6,:) = [];'); % remove rows 4-6
B = h.GetWorkspaceData('A', 'base')

MATLAB displays:

B =
    0.6208    0.2344    0.6273    0.3716    0.7764    0.7036
    0.7313    0.5488    0.6991    0.4253    0.4893    0.4850
    0.1939    0.9316    0.3972    0.5947    0.1859    0.1146

Using Feval

Use the Feval function when you want the server to execute commands that you cannot express in a single string. The following example uses variables defined in the client, rows and cols, to modify the server.

This is a continuation of the previous example:

rows = 6;   cols = 3;
h.Feval('reshape', 0, 'A=', rows, cols);

MATLAB interprets A in the expression 'A=' as a server variable name.
The reshape function in the previous statement does not make an assignment to the server variable A; it is equivalent to the following MATLAB statement:

reshape(A,6,3)

which returns a result, but does not assign the new array. If you get the variable A from the server, it is unchanged:

B = h.GetWorkspaceData('A', 'base')

MATLAB displays:

B =
    0.6208    0.2344    0.6273    0.3716    0.7764    0.7036
    0.7313    0.5488    0.6991    0.4253    0.4893    0.4850
    0.1939    0.9316    0.3972    0.5947    0.1859    0.1146

Use the Feval function return value to get the result of this type of operation. For example, the following statement reshapes the server-side array A and returns the result of this operation in the client-side variable a:

a = h.Feval('reshape', 1, 'A=', rows, cols);

The Feval function returns a cell array. To view the contents, type:

a{:}

MATLAB displays:

ans =
    0.6208    0.6273    0.7764
    0.7313    0.6991    0.4893
    0.1939    0.3972    0.1859
    0.2344    0.3716    0.7036
    0.5488    0.4253    0.4850
    0.9316    0.5947    0.1146

(http://www.mathworks.com/)

Menggabungkan Dengan Delphi

Setelah kita mengetahui beberapa fungsi diatas, selanjutnya kita coba menggabungkan dengan program delphi yang akan kita buat berikut :

Langkah-Langkah

  • Buka Program Delphi (nb : pada contoh digunakan delphi 7)
  • Pada form, letakkan komponen : 1 memo, 3 button.
  • Untuk Selanjutnya Lihat Source Code dibawah ini

Source Code

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  madelph : variant;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
application.Terminate;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
memo1.Clear;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
madelph := CreateOleObject('matlab.application');
end;

procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
begin
  for i:= 0 to memo1.Lines.Count-1 do
   begin
   madelph.execute(memo1.Lines[i]);
   end;
end;

end.

ScreenShoot :

Refferensi :

http://google.com/

http://mathworks.com/

http://delphibasics.co.uk/

Download Full Source Code + EXE :

http://www.4shared.com/file/LprQWdG3/Matlab_Delphi_Source.html

Program ini dijalankan pada system operasi windows, dibuat dengan perangkat lunak borland delphi versi 7 dan perangkat lunak matlab versi 7.  Sekian tulisan sederhana ini, ibarat pepatah “Kalau ada sumur diladang boleh kita menumpang mandi, kalau ada waktu luang ijinkan saya menulis lagi….

Semoga bermanfaat

(Soerabaja, 12 Juni 2010)

Regards,


Actions

Information

10 responses

24 06 2010
jefry

Thanks bwt ilmunya pak…

25 06 2010
heru widakdo

ok, sama2…

29 08 2010
vina

Mas mo nanya nih, klo matlab nya bukan yg versi 7 apa bisa ?

29 08 2010
heru widakdo

@vina : mungkin saja bisa…. tidak harus menggunakan delphi versi 7 ataupun matlab versi tsb. Hanya kebetulan saja saya memiliki versi itu. Silakan di coba dulu.
Thanks 4 Comment.

30 05 2012
Dany Ryzky Arif Saputra

bagus mas tutorialnya

15 10 2012
heru widakdo

Thanks for comment mas dany

15 10 2012
agung

bagus bnget, n thank telah berbagi…😀

15 10 2012
heru widakdo

Sama2 mas agung, thanks for comment

20 10 2012
Abdam

kok linknya tidak bisa ya, tolong diperbaiki

18 03 2015
uswah

maz lw fungsix kita buat di matlab apa bisa GUI nya kita buat di delphi?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: