Calling User-defined Script from Java

조회 수: 6 (최근 30일)
Simos Gerasimou
Simos Gerasimou 2017년 9월 5일
댓글: Manuel Lindorfer 2018년 11월 23일
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(https://uk.mathworks.com/help/matlab/matlab_external/java-example-source-code.html) 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(https://uk.mathworks.com/matlabcentral/answers/322114-calling-script-from-python) but unfortunately, do not work in Java.
Any ideas how to achieve this functionality?
  댓글 수: 2
Amina Rahman
Amina Rahman 2017년 9월 7일
Did you mean this solution?
Simos Gerasimou
Simos Gerasimou 2017년 9월 7일
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!!

댓글을 달려면 로그인하십시오.

답변 (1개)

Amit Doshi
Amit Doshi 2017년 9월 8일
Hello,
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
ml.eval("myFunction(args)");
  댓글 수: 2
Jiss George
Jiss George 2018년 4월 22일
can u please give more explanation, when i am executing this ml.eval("cd path/to/funtion"); am getting error as cd can't cd to directory.
Manuel Lindorfer
Manuel Lindorfer 2018년 11월 23일
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, ...);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call MATLAB from Java에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by