How to pass array to MatLab executable in Java
Afficher commentaires plus anciens
I am trying to pass an array to a compiled Matlab Function (Executable), but struggling to find the correct format. Below is my simple MatLab example that I am testing:
function addition = Addition(numbers)
addition = 0;
for i = 1:length(numbers)
addition = addition + str2double(numbers(i));
end
disp(addition);
end
The function takes in an array of numbers and adds them then displays the answer at the end. I am trying to call this MatLab executable from Java utilizing the following code:
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class MatLabTest {
public static void main(String[] args) {
MatLabTest obj = new MatLabTest();
String[] numbers = {"10","10","10"};
String command = "C:\\Users\\user\\Documents\\MATLAB\\Addition.exe " + numbers;
String output = obj.executeCommand(command);
System.out.println(output);
}
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p = null;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line.replaceAll("\\s+","") + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
The code just takes in a command as a string and runs it via command line on Windows. The issue is it does not recognize the array of numbers I try to pass to MatLab executable.
Can anyone help?
Thanks
Réponses (0)
Catégories
En savoir plus sur Java Package Integration dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!