Create Java Phone Book Application Using Structure Array
In this example, you create a Java® package that calls a MATLAB® function to modify a structure array and implement a phone book application.
Files
MATLAB Function | makephone.m
|
MATLAB Function Location |
|
Java Code Location |
|
Procedure
Copy the
PhoneExample
folder that ships with MATLAB to your work folder:copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','PhoneExample'),'PhoneExample')
At the MATLAB command prompt, navigate to the new
PhoneExample\PhoneDemoComp
subfolder in your work folder.Examine the
makephone.m
function.function book = makephone(friends) book = friends; for i = 1:numel(friends) numberStr = num2str(book(i).phone); book(i).external = ['(508) 555-' numberStr]; end
The function takes a structure array as an input, modifies it, and supplies the modified array as an output.
Build the Java package with the Library Compiler app or
compiler.build.javaPackage
using the following information:Field Value Library Name phonebookdemo
Class Name phonebook
File to Compile makephone.m
For example, if you are using
compiler.build.javaPackage
, type:buildResults = compiler.build.javaPackage('makephone.m', ... 'PackageName','phonebookdemo', ... 'ClassName','phonebook');
For more details, see the instructions in Generate Java Package and Build Java Application.
Write source code for an application that accesses the MATLAB functions.
The sample application for this example is in
PhoneExample\PhoneDemoJavaApp\getphone.java
.The program does the following:
Creates a structure array, using
MWStructArray
to represent the example phonebook data.Instantiates the plotter class as
thePhonebook
object:thePhonebook = new phonebook();
Calls the
makephone
method to create a modified copy of the structure by adding an additional field:result = thePhonebook.makephone(1, friends);
Uses a
try-catch
block to catch and handle any exceptions.
In MATLAB, navigate to the
PhoneExample\PhoneDemoJavaApp
folder.Copy the generated
phonebookdemo.jar
package into this folder.If you used
compiler.build.javaPackage
, type:copyfile(fullfile('..','PhoneDemoComp','phonebookdemojavaPackage','phonebookdemo.jar'))
If you used the Library Compiler, type:
copyfile(fullfile('..','PhoneDemoComp','phonebookdemo','for_testing','phonebookdemo.jar'))
In a command prompt window,
cd
to thePhoneDemoJavaApp
folder.Compile the
getphone
application usingjavac
.On Windows®, type:
javac -classpath "
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\phonebookdemo.jar getphone.javaOn UNIX®, type:
javac -classpath "
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./phonebookdemo.jar getphone.java
Replace
with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Linux®, the path may bematlabroot
/usr/local/MATLAB/R2024b
.Run the
getphone
application.On Windows, type:
java -classpath .;"
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\phonebookdemo.jar getphoneOn UNIX, type:
java -classpath .:"
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./phonebookdemo.jar getphoneNote
If you are running the application on the Mac 64-bit platform, you must add the
-d64
flag in the Java command.
The
getphone
program displays the following output:Friends: 2x2 struct array with fields: name phone Result: 2x2 struct array with fields: name phone external Result record 2: Mary Smith 3912 (508) 555-3912 Entire structure: Number of Elements: 4 Dimensions: 2-by-2 Number of Fields: 3 Standard MATLAB view: 2x2 struct array with fields: name phone external Walking structure: Element 1 name: Jordan Robert phone: 3386 external: (508) 555-3386 Element 2 name: Mary Smith phone: 3912 external: (508) 555-3912 Element 3 name: Stacy Flora phone: 3238 external: (508) 555-3238 Element 4 name: Harry Alpert phone: 3077 external: (508) 555-3077
See Also
compiler.build.javaPackage
| Library Compiler