Can I make a call to a third party Matlab function while using the Matlab Engine API along with Java?
3 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(1 个)
Bo Li
2017-3-15
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Java Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!