Results 1 to 6 of 6
  1. #1
    npoorni is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default run a matlab script from java

    How to run a matlab script from java code??

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Runtime.getRuntime.exec() ?

    ProcessBuilder ?

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I have a Matlab class I wrote many years back that does this very thing. I'm sure I would do things differently today, but it does work. You are welcome to use it, but I wouldn't mind if I got a bit of credit.

    Java Code:
    public class Matlab 
    {
      /**
       * The underlying process
       */
       Process matlabProc = null;
    
      /**
       * PrintStream for matlab responses
       */
       PrintStream reponseStream = null;
    
      /**
       * Response handler
       */
       ResponseHandler handler = null;
    
      /**
       * Class to handle responses
       */
       class ResponseHandler extends Thread {
    
        boolean keepGoing = true;
    
        public void run() {
    
          byte[] bytes = new byte[4096];
    
          while(keepGoing) {
    
    	try {
    	  if(matlabProc != null) {
    	    if(reponseStream != null) {
    
    	      InputStream in = matlabProc.getInputStream();
    	      int numBytes = in.read(bytes);
    	
                 //need a check here for numBytes == -1
          
    	      reponseStream.write(bytes, 0, numBytes);
    
    	    } else {
    	      try {
    		sleep(500);
    	      } catch (Exception ex2) {}
    	    }
    
    	  } else {
    	    try {
    	      sleep(500);
    	    } catch (Exception ex2) {}
    	  }
    	  
    	} catch(Exception ex) {
    	  try {
    	    sleep(500);
    	  } catch (Exception ex2) {}
    	}	
          }
    
        }
    
        public void destroy() {
          keepGoing = false;
        }
    
      }
    
      /**
       * Constructor
       */
      public Matlab() {
        handler = new ResponseHandler();
        handler.start();
    
        spawn();
      }
    
      /** 
       * Destructor
       */
      public void destroy() {
        handler.destroy();
    
        matlabProc.destroy();
        try {
          matlabProc.waitFor();
        } catch (Exception ex) {}
      }
    
      /**
       * Check to see if the session still exists. If not, recreate it.
       */
      public void validateSession() {
        boolean startMatlab = false;
    
        try {
          matlabProc.exitValue(); 
          startMatlab = true;
        } catch (IllegalThreadStateException ex){
          startMatlab = false;
        }    
    
        if(startMatlab)
          spawn();
      }
    
      /**
       * Set the print stream that responses should go to.
       * Set to null if none is desired.
       */
      public void setPrintStream(PrintStream reponseStream) {
        this.reponseStream = reponseStream;
      }
    
      /**
       * Execute command(s) in matlab session
       */
      public synchronized void exec(String cmd) throws java.io.IOException {
    
        OutputStream out = matlabProc.getOutputStream();
        
        out.write(cmd.getBytes());
        out.flush();    
      }
    
      /**
       * Spawn a matlab session
       */
      protected void spawn() {
        try {
          matlabProc = Runtime.getRuntime().exec("matlab");
        } catch (Exception ex) {ex.printStackTrace();}
      }
    }

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    There are libraries that allow you to control Matlab from Java. Google for them.

  5. #5
    pradyumna is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Question running matlab script from java

    hello friends,

    i am doing a project on steganography which i am implementing using MATLAB. and i want to build a interface using JAVA swings through which a user can run MATLAB code .....

    i want to know is it possible to run matlab script from JAVA(in particular through such interface)???

    PLS HELP!!!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Old zombie thread being re-animated. I am locking this.

    pradyumna, welcome to the forum. I invite you to re-ask your question in a new thread. Also, please let us know the results of any Google search you've done to help solve this (as per OrangeDog's excellent recommendation). Much luck, and again, welcome.

Similar Threads

  1. run perl script from java
    By maheshmhs in forum New To Java
    Replies: 0
    Last Post: 12-19-2008, 01:40 PM
  2. Help, Integrate a Java with Matlab
    By toby in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2008, 08:08 AM
  3. Replies: 3
    Last Post: 07-23-2008, 09:07 AM
  4. Replies: 3
    Last Post: 04-10-2008, 10:01 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •