Main Content

Pass Variables fromMATLABtoJava

Use theMatlabEnginegetVariableorgetVariableAsyncmethods to get variables from the MATLAB®base workspace. To determine the appropriate mapping of MATLAB type to Java®type, seeJava Data Type Conversions.

Coordinate Conversion

This example code uses the MATLABcart2sphfunction to convert from Cartesian to spherical coordinates. ThegetVariablemethod gets the returned spherical coordinate variables from the MATLAB base workspace.

import com.mathworks.engine.*; public class GetPolar { public static void main(String[] args) throws Exception { MatlabEngine eng = MatlabEngine.startMatlab(); eng.eval("[az,el,r] = cart2sph(5, 7, 3);"); double az = eng.getVariable("az"); double el = eng.getVariable("el"); double r = eng.getVariable("r"); System.out.println("Azimuth: " + az); System.out.println("Elevation: " + el); System.out.println("Radius " + r); eng.close(); } }

Related Topics