remo
remo

Reputation: 896

How to redirect output from java to matlab

I have a compiled .JAR file which gets two datasets as input and results in some scores (real numbers). The outputs are printed in console (cmd prompt env.). However I want to call the function multiple times whitin my matlab code. I have no access to the source code of JAR file. Is there any efficient way (probably other than using files) to get the results of jar file back to matlab.

Sample JAR outputs:


a: 82.212 %

b: 0 %

c: 0.003 %


d: 13.471 %

e: 4.809 %


I want these 5 numbers. They are always printed in the same format.

Is there any way I can redirect these results for example to a TCP/IP port that matlab can read the results?

Thanks

Upvotes: 0

Views: 404

Answers (2)

Andrew Janke
Andrew Janke

Reputation: 23888

Another option to consider is to link the JAR in to your Matlab program by putting it on the javaclasspath. Then you can call methods on any of the public objects defined in the JAR directly from your M-code. If there's an appropriate entry point to calculate those values, you could call the calculation method directly instead of going through the JAR's main, and avoid the overhead of shelling out to a separate JVM process and parsing the output. If you end up calling it a lot, it'll be more efficient.

Upvotes: 2

Serg
Serg

Reputation: 14118

Use the second output result from system() function. It will contain the required output string that you need to parse.

[status, result] = system('command');

Now, assuming you have the result string, here how you can parse it into vector of values:

names = {'a', 'b', 'c', 'd', 'e'}';
values = nan(size(names));
for k = 1 : length(names)
    idx = strfind(result, names{k});
    assert(numel(idx) == 1);
    values(k) = sscanf(result(idx + length(names{k}) + 1 : end), '%f');
end

Upvotes: 2

Related Questions