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

4 ビュー (過去 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
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;
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);
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(

回答 (1 件)

Bo Li
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.
  1 件のコメント
manan lalit
manan lalit 2017 年 3 月 15 日
編集済み: manan lalit 2017 年 3 月 15 日
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?


Community Treasure Hunt

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

Start Hunting!

Translated by