Main Content

Execute MATLAB Functions from Java

Calling MATLAB Functions

You can execute MATLAB® functions from Java® using the MatlabEngine feval and fevalAsync methods. These methods work like the MATLAB feval function. Use feval and fevalAsync when you want to return the result of the function execution to Java or to pass arguments from Java.

To call a MATLAB function:

  • Pass the function name as a string.

  • Define the input arguments required by the MATLAB function.

  • Specify the number of outputs expect from the MATLAB function (1 is assumed if not specified).

  • Define the appropriate returned type for the outputs of the MATLAB function.

  • Use writers to redirect output from the MATLAB command window to Java.

You can also use the MatlabEngine eval and evalAsync methods to evaluate MATLAB expressions. These methods enable you to create variables in the MATLAB workspace, but do not return values.

Execute Function with Single Returned Argument

This example code uses the MATLAB sqrt function to find the square root of the elements in an array of doubles. The feval method returns a double array containing the results of the sqrt function call.

import com.mathworks.engine.*;

public class javaFevalFunc{
    public static void main(String[] args) throws Exception{
        MatlabEngine eng = MatlabEngine.startMatlab();
        double[] a = {2.0 ,4.0, 6.0};
        double[] roots = eng.feval("sqrt", a);
        for (double e: roots) {
            System.out.println(e);
        }
        eng.close();
    }
}

Execute Function with Multiple Returned Arguments

This example code uses the MATLAB gcd function to find the greatest common divisor and Bézout coefficients from the two integer values passed as input arguments. The feval method returns an object array containing the results of the gcd function call. The returned values are integers.

Because the MATLAB gcd function is returning three output arguments, specify the number of returned values as the first argument to the feval method.

import com.mathworks.engine.*;

public class javaFevalFcnMulti {
    public static void main(String[] args) throws Exception {
        MatlabEngine eng = MatlabEngine.startMatlab();
        Object[] results = eng.feval(3, "gcd", 40, 60);
        Integer G = (Integer)results[0];
        Integer U = (Integer)results[1];
        Integer V = (Integer)results[2];
        eng.close();
    }
}

When to Specify Number of Output Arguments

The MatlabEngine feval and fevalAsync methods enable you to specify the number of output arguments returned by the MATLAB function. By default, the number of output arguments from the MATLAB function is assumed to be 1.

If you want to call a MATLAB function with no outputs or more than one output, specify that number as the first argument passed to feval or fevalAsync.

For example, this code calls the MATLAB gcd function with the three output argument syntax:

Object[] results = eng.feval(3, "gcd", 40, 60);

MATLAB functions can behave differently depending on the number of outputs requested. Some functions can return no outputs or a specified number of outputs. For example, the MATLAB pause function holds execution for a specified number of seconds. However, if you call pause with an output argument, the function returns immediately with a status value. Therefore, this code does not cause MATLAB to pause because feval requests one output argument.

eng.feval("pause", 10);

To pause MATLAB execution for the 10 seconds requested, specify the number of outputs as 0.

eng.feval(0, "pause", 10);

Note

To ensure that a MATLAB function is called with no outputs, specify the number of returned arguments as 0.

Related Topics