Can I make a call to a third party Matlab function while using the Matlab Engine API along with Java?

4 views (last 30 days)
I wish to use the MATLAB Engine API for running calculations along side a graphical user interface developed in Java on Eclipse IDE. Was wondering if it may be possible to make a call to third party functions written in MATLAB? Is there a way to zip a non-standard function along with the engine.jar found at the location: C:\Program Files\MATLAB\R2016b\extern\engines\java\jar ?
As a simple example, I would like to be able to run the function called 'apowb.m' which accepts two variables x1 and x2 and computes y=x1^x2.
function [ y ] = apowb( x1, x2 )
% This function evaluates y = x1 ^ x2
y=x1^(x2);
end
I make a reference to this function in my controller method in Java. Some snippets from that method are displayed below:
import com.mathworks.engine.MatlabEngine;
...
public class PersonOverviewController {
private static Future<MatlabEngine> engFuture;
private static MatlabEngine eng;
...
@FXML
private void calculate() throws Exception{
engFuture = MatlabEngine.startMatlabAsync();
eng = engFuture.get();
double input1=5;
double input2=6;
Future<Double> future = eng.fevalAsync("apowb", input1, input2);
System.out.println(future.get());
}
}
This throws a `null pointer exception' probably because the function "apowb" is not traced by the Java Compiler?
Caused by: java.lang.NullPointerException
at manan.gui.view.PersonOverviewController.calculate(PersonOverviewController.java:117)

Answers (1)

Bo Li
Bo Li on 15 Mar 2017
Did you add "apowb" to the MATLAB path? You can do that using addpath either in MATLAB or through feval in Java. The M file needs to be searchable in order to be found by Java Engine.
  1 Comment
manan lalit
manan lalit on 15 Mar 2017
Edited: manan lalit on 15 Mar 2017
I tried adding the function to the current matlabpath by using 'addpath' command in MATLAB, but I get the same error as before, upon running the java file in ECLIPSE:
Undefined function or variable 'apowb'.
Do you know if I can specify the location of apowb.m somewhere in Eclipse? Do you know if another option could be - compiling 'apowb.m' into a '.jar' format and then adding it to Eclipse as an external library?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!