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 location |
|
java code location |
|
procedure
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.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
create a java package that encapsulates
makesqr.m
by using the library compiler app or .use the following information for your project:
package name magicsquare
class name magic
file to compile makesqr.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.
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 datainstantiates a
magic
objectcalls 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 argumentsuses a
try
-catch
block to handle exceptionsfrees native resources using
mwarray
methods
in matlab, navigate to the
magicdemojavaapp
folder.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'))
in a system command window, navigate to the
plotdemojavaapp
folder.compile the java application using
javac
.on windows®, execute this command:
javac -classpath "
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\magicsquare.jar getmagic.javaon unix®, execute this command:
javac -classpath "
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./magicsquare.jar getmagic.java
replace
with the path to your matlab or matlab runtime installation folder. for example, on windows, the path may bematlabroot
c:\program files\matlab\r2023a
.for more details, see compile and run matlab generated java application.
from the system command prompt, run the application.
on windows, type:
java -classpath .;"
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\magicsquare.jar getmagic 5on 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
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
| | | |