Thursday, January 11, 2007

Java - Matlab Connection
11 Jan 2007

Here is a small demonstration program which allows you to type the text:
a = [1 2 3];
in a java application (launched from within matlab) and send it into Matlab's workspace as matrix data.

This is how you do it:

1. Download the MatlabInterface.jar [or the Netbeans Project MatlabInterface]

2. To Compile your own code add jmi.jar [%MATLABROOT%/java/jar] from the following directory in Project Properties->Libraries->Add JAR/zip

3.
Matlab Command Window:





>> javaaddpath('C:\Debprakash\Java\MatlabInterface\dist\MatlabInterface.jar');
>> import org.test.MatDialog;
>> MatDialog.test
r = [98 93 52 39 31 17 10 39 43 97 ];
>> r
r =

98 93 52 39 31 17 10 39 43 97


Java Code for MatDialog.java


Create the Matlab object which connects to the current matlab session.
-----------------------------------------------------------------------


package org.test;

import java.util.Date;
import com.mathworks.jmi.*;

public class MatDialog extends javax.swing.JFrame
{
private Matlab matlab;
public MatDialog()
{
...
matlab = new Matlab();
}



Add an action listener to call eval on the matlab object
------------------------------------------------------


...
private void jButtonProcessActionPerformed(java.awt.event.ActionEvent evt)
{
...
else if (jRadioButton2.isSelected())
{
cmd = jTextField1.getText();
}

if (cmd != null)
{
System.out.println(cmd);
matlab.eval(cmd);
}
}




For More Info