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
PhoneExamplefolder 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\PhoneDemoCompsubfolder in your work folder.Examine the
makephone.mfunction.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 Java Package Compiler app or
compiler.build.javaPackageusing the following information:Field Value Java Package Name phonebookdemoClass Name phonebookFile to Compile makephone.mFor 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
MWStructArrayto represent the example phonebook data.Instantiates the plotter class as
thePhonebookobject:thePhonebook = new phonebook();Calls the
makephonemethod to create a modified copy of the structure by adding an additional field:result = thePhonebook.makephone(1, friends);Uses a
try-catchblock to catch and handle any exceptions.
In MATLAB, navigate to the
PhoneExample\PhoneDemoJavaAppfolder.Copy the generated
phonebookdemo.jarpackage into this folder.For instance, if you used
compiler.build.javaPackage, type:copyfile(fullfile('..','PhoneDemoComp','phonebookdemojavaPackage','phonebookdemo.jar'))
In a command prompt window,
cdto thePhoneDemoJavaAppfolder.Compile the
getphoneapplication 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/R2025b.Run the
getphoneapplication.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
-d64flag in the Java command.
The
getphoneprogram 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 | Java Package
Compiler
