hi there, i need to call some matlab functions from java and i've found useful tips on this site, but i have got some problems when trying them on a simple example :S i just want to pass a number as an argument for the function i'm calling, but can't get a good result!
Process p= Runtime.getRuntime().exec("C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe 100" );
i have tried with a string array and a lot of different ways as well... but no idea
cmd[0]= "C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe"; cmd[1]= "100";
Process p= Runtime.getRuntime().exec(cmd);
any ideas? could someone help me?? thanks in advance dani
If you're on Windows, you should probably use backslash \ rather than forward slash /. Since it's in a Java literal you will have to double each backslash. Also, you may need to put quotes around the whole path, because of the spaces inside "Documents and Settings". Those quotes will also need to be escaped with backslashes. So:
"I'm not back." - Bill Harding, Twister
Maki Jav
Ranch Hand
Joined: May 09, 2002
Posts: 423
posted
1
Hi,
Please first of all see whether you can pass argument to matlab from command prompt You may see this post of mine at Other Java APIs forum for help as you are on windows. I have posted a code to run bat file which in turns execute command.
Maki Jav
Help gets you when you need it!
daniel fuertes
Greenhorn
Joined: Jan 15, 2008
Posts: 3
posted
0
hi! thank you very much for your answers. as i can see, it's not a problem of the slashes, because i can call matlab functions with no problem and read the sentences displayed when sending no arguments, the problem comes if i send an argument...
Process p= Runtime.getRuntime().exec("C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe 100");
_________________________________________ String [] parametros = new String[3]; parametros[0]= "C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe" ; parametros[1]= ""; parametros[2]= "100";
Process p= Runtime.getRuntime().exec( parametros ); _________________________________________ String [] parametros = new String[2]; parametros[0]= "C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe" ; parametros[1]= "100";
Process p= Runtime.getRuntime().exec( parametros );
if i do any these posibilities, "100" as an argument, i get three numbers as a result (trying with different numbers, i get as many result numbers as digits in my argument), when i'm just expecting one!!!
hope somebody knows what's going on here.... thank you very much
daniel fuertes
Greenhorn
Joined: Jan 15, 2008
Posts: 3
posted
0
i found out the problem is not when passing arguments, the problem is the matlab .exe. i dont know why but it converts in ascii code all the inputs, so i get strange numbers at the output :S
thanks for the answers
Maki Jav
Ranch Hand
Joined: May 09, 2002
Posts: 423
posted
0
See if there is a way on the Java side to send data as ascii.
Maki Jav
Joshua Kaplan
Greenhorn
Joined: Jul 08, 2011
Posts: 5
posted
0
An easier way than using Runtime.exec() is to make use of the matlabcontrol Java library. You can use feval to directly call MATLAB functions and provide any number of arguments directly. To get started, take a look at the walkthrough.