Main Content

Use MATLAB Class in Java Application

Overview

This example shows you how to create a Java® application that calls MATLAB® wrapper functions for a MATLAB class.

In this example, you perform the following steps:

  1. Use MATLAB Compiler SDK™ to create a package that uses MATLAB wrapper functions to access a MATLAB class.

  2. Call the MATLAB wrapper functions in a Java application.

  3. Build and run the application.

Procedure

  1. In MATLAB, examine the MATLAB code that you want to package. For this example, create a MATLAB class named MyMATLABClass.m using the following code:

    classdef MyMatlabClass < handle
        
        properties (Access = private)
            x % input variable
            y % input variable
            z % result variable
        end
        
        methods
            function this = MyMatlabClass()
                this.x = []; this.y = [];
            end
            
            function setInput(this, input)
                input = input(:);           
                if isnumeric(input) && numel(input) == 2
                    this.x = input(1);
                    this.y = input(2);
                end
            end
            
            function result = getResult(this)
                result = this.z;
            end
            
            function status = compute(this)
                try
                    this.z = (this.x.^2 + this.y.^2)^0.5;
                    status = true;
                catch
                    status = false;
                end
            end
        end
        
    end
  2. Create four MATLAB wrapper functions for the class: CreateMyMATLABClass.m, SetInput.m, Compute.m, and GetResult.m.

    CreateMyMATLABClass.m.  

    function instance = CreateMyMATLABClass()
        instance = MyMATLABClass();
    end

  3. Build the Java package with the Library Compiler app or compiler.build.javaPackage using the following information:

    FieldValue
    Library NameMyMATLABClass1
    Class NameClass1
    Files to CompileCreateMyMATLABClass.m
    SetInput.m
    Compute.m
    GetResult.m

    For example, if you are using compiler.build.javaPackage, type:

    buildResults = compiler.build.javaPackage(["CreateMyMATLABClass.m", ...
    "SetInput.m","Compute.m","GetResult.m"], ...
    'PackageName','MyMATLABClass1', ...
    'ClassName','Class1');

    For more details, see the instructions in Generate Java Package and Build Java Application.

    Note

    You do not need to manually add the MyMATLABClass.m file to the package, as the compiler automatically includes it during dependency analysis.

  4. Navigate to the folder that contains the generated MyMATLABClass1.jar package. If you used the Library Compiler, the package is in the for_testing folder.

  5. Write source code for an application that accesses the MATLAB functions. The code for this example is provided below.

     javadriver.java

  6. Open a command prompt window and navigate to the folder that contains javadriver.java and MyMATLABClass.jar.

  7. Compile the javadriver.java application using javac.

    • On Windows®, type:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\MyMATLABClass.jar javadriver.java

    • On UNIX®, type:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./MyMATLABClass.jar javadriver.java

    Replace matlabroot with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Linux®, the path may be /usr/local/MATLAB/R2024b.

  8. Run the javadriver application.

    On Windows, type:

    java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\MyMATLABClass.jar javadriver

    On Linux, type:

    java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":.\MyMATLABClass.jar javadriver

    Note

    If you are running the application on the Mac 64-bit platform, you must add the -d64 flag in the Java command.

The javadriver program displays the following output:

--- USE: Constructors --- 
1 
2.2361 
--- Done. --- 

See Also

|

Related Topics