integrate simple matlab function into java application -凯发k8网页登录

main content

integrate simple matlab function into java application

this example shows how to invoke a matlab® method that generates a magic square in a java® application.

files

matlab function locationmatlabroot\toolbox\javabuilder\examples\magicsquareexample\magicdemocomp\makesqr.m
java code locationmatlabroot\toolbox\javabuilder\examples\magicsquareexample\magicdemojavaapp\getmagic.java

procedure

  1. copy the magicsquareexample folder that ships with matlab to your work folder:

    copyfile(fullfile(matlabroot,'toolbox','javabuilder','examples','magicsquareexample'))

    at the matlab command prompt, navigate to the new magicsquareexample\magicdemocomp subfolder in your work folder.

  2. examine the makesqr.m function.

    function y = makesqr(x)
    y = magic(x);

    at the matlab command prompt, enter makesqr(5).

    the output is a 5-by-5 matrix.

        17    24     1     8    15
        23     5     7    14    16
         4     6    13    20    22
        10    12    19    21     3
        11    18    25     2     9
  3. create a java package that encapsulates makesqr.m by using the library compiler app or .

    use the following information for your project:

    package namemagicsquare
    class namemagic
    file to compilemakesqr.m

    for example, if you are using compiler.build.javapackage, type:

    buildresults = compiler.build.javapackage('makesqr.m', ...
        'packagename','magicsquare', ...
        'classname','magic');

    for more details, see the instructions in generate java package and build java application.

  4. write source code for a java application that accesses the matlab function.

    the sample application for this example is in magicsquareexample\magicdemojavaapp\getmagic.java.

     

    the program does the following:

    • creates an mwnumericarray array to store the input data

    • instantiates a magic object

    • calls the makesqr method, where the first parameter specifies the number of output arguments and the following parameters are passed to the function in order as input arguments

    • uses a try-catch block to handle exceptions

    • frees native resources using mwarray methods

  5. in matlab, navigate to the magicdemojavaapp folder.

  6. copy the generated magicsquare.jar package into this folder.

    • if you used compiler.build.javapackage, type:

      copyfile(fullfile('..','magicdemocomp','magicsquarejavapackage','magicsquare.jar'))
    • if you used the library compiler, type:

      copyfile(fullfile('..','magicdemocomp','magicsquare','for_testing','magicsquare.jar'))
  7. in a system command window, navigate to the plotdemojavaapp folder.

  8. compile the java application using javac.

    • on windows®, execute this command:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\magicsquare.jar getmagic.java
    • on unix®, execute this command:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./magicsquare.jar getmagic.java

    replace matlabroot with the path to your matlab or matlab runtime installation folder. for example, on windows, the path may be c:\program files\matlab\r2023a.

    for more details, see compile and run matlab generated java application.

  9. from the system command prompt, run the application.

    • on windows, type:

      java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\magicsquare.jar getmagic 5
    • on unix, type:

      java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./magicsquare.jar getmagic 5

    the application outputs a 5-by-5 magic square in the command window.

        17    24     1     8    15
        23     5     7    14    16
         4     6    13    20    22
        10    12    19    21     3
        11    18    25     2     9
  10. to follow up on this example:

    • try running the generated application on a different computer.

    • try building an installer for the package using .

    • try integrating a package that consists of multiple functions.

see also

| | | |

related topics

网站地图