MATLAB Answers

Calling User-defined Script from Java

25 views (last 30 days)
Simos Gerasimou
Simos Gerasimou on 5 Sep 2017
Commented: Manuel Lindorfer on 23 Nov 2018
I've installed R2017a and try to use Matlab Engine API for Java to run a user-defined script/function from Java.
The examples provided here( work perfectly fine. These examples, however, make use of Matlab package functions such as gcd, max etc.
I can't find any examples that enable to import and execute Matlab scripts (functions) defined by a user. Some examples for Python are provided here( but unfortunately, do not work in Java.
Any ideas how to achieve this functionality?
Simos Gerasimou
Simos Gerasimou on 7 Sep 2017
I tried the solution suggested at SO using matlabcontrol 4.1.0, but unfortunately, it does work for Matlab2017a.
Also, since a Java API for Matlab has been released, I expected that the functionality to do what matlabcontrol does would be supported as well!!

Sign in to comment.

Answers (1)

Amit Doshi
Amit Doshi on 8 Sep 2017
You can call your function using 'eval'.
For example,
//Start MATLAB asynchronously
Future<MatlabEngine> eng = MatlabEngine.startMatlabAsync();
// Get engine instance
MatlabEngine ml = eng.get();
// Evaluate the command to cd to your function
ml.eval("cd path/to/funtion");
// Evaluate the function
Manuel Lindorfer
Manuel Lindorfer on 23 Nov 2018
You have to specify the apostrophes properly when executing the command, similar to when executing it within the Matlab environment. For example,
// Start Matlab
Future<MatlabEngine> engine = MatlabEngine.startMatlabAsync();
MatlabEngine eng = engine.get();
// Change directory and evaluate your function
eng.eval("cd 'path/to/your/function'");
eng.feval("yourFunction", param1, param2, ...);

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!