f



Problem with Java/MATLAB app on Windows

I have a tool that we use in-house that I wrote in Java 
that uses Matlab as a math and plotting engine. On the unix 
version of the software, I can have my Java application 
start a child MATLAB process and the program can interact 
with it via standard i/o as though one were using the 
MATLAB command line normally.

Now I want to run the tool on Windows.

This technique fails on Windows as the standard i/o doesn't 
appear to be used by MATLAB on that platform. So I switched 
approaches to calling Java methods directly within MATLAB, 
and using the Mathworks-provided callback interface to get 
commands back to the host MATLAB process. I instantiate the 
fundamental class of my application from the MATLAB command 
line. This results in my Java UI being displayed, while the 
command line shows a new prompt and is still ready for 
input.

The problem is when I try to use the tool to generate a new 
MATLAB figure window both my Java app and MATLAB itself 
hard-freeze and have to be killed via the OS.

If anyone has any information about this issue and how to 
resolve it, or an idea of any other method of getting a 
Java GUI application to drive MATLAB, please let me know!

Simple code that should demonstrate the failure is provided 
below. My Windows MATLAB version: 7.1.0.183 (R14) SP3

-Zack

------DEMO CODE-------

import com.mathworks.jmi.* ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.JButton ;
import javax.swing.JFrame ;

public class BugFrame extends JFrame implements 
ActionListener {
  public Matlab MatlabInterface = new Matlab() ;

  public BugFrame() {
    JButton bugbutton = new JButton( "Crash Now" ) ;
    bugbutton.addActionListener( this ) ;
    this.getContentPane().add( bugbutton ) ;
    this.pack() ;
    this.show() ;
  }

  public void actionPerformed( ActionEvent ev ) {
    try {
      this.MatlabInterface.eval( "figure" ) ;
    }
    catch ( Exception e ) {}
  }
}
0
6/19/2008 10:15:19 PM
comp.soft-sys.matlab 211266 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

2 Replies
380 Views

Similar Articles

[PageSpeed] 40

I gave the wrong version of MATLAB. My Windows version is 
7.4.0.287 (R2007a).
0
6/20/2008 12:10:23 AM
"Zack Stewart" <Zachary.Stewart@miscnonsense.gd-ais.com>
wrote in message <g3elpn$7d3$1@fred.mathworks.com>...
> I have a tool that we use in-house that I wrote in Java 
> that uses Matlab as a math and plotting engine. On the unix 
> version of the software, I can have my Java application 
> start a child MATLAB process and the program can interact 
> with it via standard i/o as though one were using the 
> MATLAB command line normally.
> 
> Now I want to run the tool on Windows.
> 
> This technique fails on Windows as the standard i/o doesn't 
> appear to be used by MATLAB on that platform. So I switched 
> approaches to calling Java methods directly within MATLAB, 
> and using the Mathworks-provided callback interface to get 
> commands back to the host MATLAB process. I instantiate the 
> fundamental class of my application from the MATLAB command 
> line. This results in my Java UI being displayed, while the 
> command line shows a new prompt and is still ready for 
> input.
> 
> The problem is when I try to use the tool to generate a new 
> MATLAB figure window both my Java app and MATLAB itself 
> hard-freeze and have to be killed via the OS.
> 
> If anyone has any information about this issue and how to 
> resolve it, or an idea of any other method of getting a 
> Java GUI application to drive MATLAB, please let me know!
> 
> Simple code that should demonstrate the failure is provided 
> below. My Windows MATLAB version: 7.1.0.183 (R14) SP3
> 
> -Zack
> 
> ------DEMO CODE-------
> 
> import com.mathworks.jmi.* ;
> import java.awt.event.ActionEvent ;
> import java.awt.event.ActionListener ;
> import javax.swing.JButton ;
> import javax.swing.JFrame ;
> 
> public class BugFrame extends JFrame implements 
> ActionListener {
>   public Matlab MatlabInterface = new Matlab() ;
> 
>   public BugFrame() {
>     JButton bugbutton = new JButton( "Crash Now" ) ;
>     bugbutton.addActionListener( this ) ;
>     this.getContentPane().add( bugbutton ) ;
>     this.pack() ;
>     this.show() ;
>   }
> 
>   public void actionPerformed( ActionEvent ev ) {
>     try {
>       this.MatlabInterface.eval( "figure" ) ;
>     }
>     catch ( Exception e ) {}
>   }
> }

The Matlab command line and your com.mathworks.jmi.Matlab
object run in the same Java thread, and it sounds like a
deadlock issue. If you Google for "MatlabControl.java" you
will find several versions of a utility for solving this
exact problem by calling Matlab from a different thread to
that of the command line.

A step beyond this which I have used for a similar
application to yourself was to use Java Remote Method
Invocation - writing a 'server' class which is started in
Matlab and a 'client' class which runs in my application.
This has the added advantage that Matlab can run in a
separate JVM to your application so an exception in one
process is unlikely to end the other process.

Pete

0
6/20/2008 6:45:03 PM
Reply: