asmaa
asmaa

Reputation:

retrieve matlab result in java

I use matlab in my project and I want to retrieve the result of matlab in java. Just I want the result. I want to retrive result of file that I make it in matlab in the java. I use this code but it give me the result in matlab windo and I want only to retrive the result in java only. this is the code

public class matlab {
    private static File myMATLABScript;
    //private static File myMATLABScript;
    public static String runScript(File scriptName) {
        String output = ""  ;
        String error = "";
        try {
            //String commandToRun = "matlab -r myMATLABScript -nodisplay < " + scriptName;
            String commandToRun = "matlab  -nosplash -r myMATLABScript -nodisplay -nodesktop < " + scriptName;

            System.out.println(commandToRun);
            Process p = Runtime.getRuntime().exec(commandToRun);
            String s;
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            // read the output from the command
            System.out.println("\nHere is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                output = s + "\n";
                System.out.println(s);
                //System.out.println("what is the problem");
            }
            // read any errors from the attempted command
            //System.out.println("\nHere is the standard error of the command (if any):\n);
            while ((s = stdError.readLine()) != null) {
                error = s + "\n";
                System.out.println(s);
            }
        } catch (Exception e) {
            System.out.println("exception happened - here’s what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }
        return output + error;
    }

    public static void main(String[] args) throws IOException{
        matlab m = new matlab();
        matlab.runScript(myMATLABScript);
    }
}

could you please help me?

Upvotes: 0

Views: 1667

Answers (4)

eternaln00b
eternaln00b

Reputation: 1053

Do you by any chance have a licence for the MATLAB Builder JA and now know it?

http://www.mathworks.com/products/javabuilder/

That would be an easy way to access the results of your MATLAB function in Java.

Upvotes: 0

Joshua Kaplan
Joshua Kaplan

Reputation: 883

matlabcontrol is a Java API which allows you to do just that. The API can be used as if you were interacting with MATLAB's Command Window, and you can retrieve the results as Java objects. There is no need to write or read input/output/error streams or external files. To run your script you would have MATLAB cd to the directory your script is in and then either eval or feval the script. To get started, take a look at the walkthrough.

Upvotes: 1

Andrew Janke
Andrew Janke

Reputation: 23888

What OS are you running on? The Matlab app behaves differently on different OSes. I assume you are on Windows because you mention the "Matlab Window" even though you're passing "-nodesktop". On Windows, Matlab is inherently a GUI app and stdin/stdout are useless.

Try changing your script to write its output to a file, determined by an environment variable, function argument you pass to mymatlabscript, or well-known path. Then read in that file instead of Matlab's stdout. Use only the "-r" switch, not "<". This will be portable across OSes and you may find it easier to parse because the output file won't have extra Matlab command window output.

You may also need to use the "-wait" switch to block until your Matlab script has completed. Normal Matlab.exe invocation on Windows will return immediately. Make sure your script ends with "exit()" or Matlab will run indefinitely, waiting for user input after your script finishes.

Upvotes: 2

Mr Fooz
Mr Fooz

Reputation: 111946

Try invoking the Matlab Engine instead of creating a new matlab process (here's another description).

Upvotes: 1

Related Questions