Main Content

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.


MATLAB Functionmakephone.m
MATLAB Function Locationmatlabroot\toolbox\javabuilder\Examples\PhoneExample\PhoneDemoComp\
Java Code Locationmatlabroot\toolbox\javabuilder\Examples\PhoneExample\PhoneDemoJavaApp\


  1. Copy the PhoneExample folder that ships with MATLAB to your work folder:


    At the MATLAB command prompt, navigate to the new PhoneExample\PhoneDemoComp subfolder in your work folder.

  2. 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];

    The function takes a structure array as an input, modifies it, and supplies the modified array as an output.

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

    Library Namephonebookdemo
    Class Namephonebook
    File to Compilemakephone.m

    For example, if you are using, type:

    buildResults ='makephone.m', ...
    'PackageName','phonebookdemo', ...

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

  4. Write source code for an application that accesses the MATLAB functions.

    The sample application for this example is in PhoneExample\PhoneDemoJavaApp\

    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.

  5. In MATLAB, navigate to the PhoneExample\PhoneDemoJavaApp folder.

  6. Copy the generated phonebookdemo.jar package into this folder.

    • If you used, type:

    • If you used the Library Compiler, type:

  7. In a command prompt window, cd to thePhoneDemoJavaApp folder.

  8. Compile the getphone application using javac.

    • On Windows®, type:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\phonebookdemo.jar
    • On UNIX®, type:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./phonebookdemo.jar

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

  9. Run the getphone application.

    • On Windows, type:

      java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\phonebookdemo.jar getphone
    • On UNIX, type:

      java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./phonebookdemo.jar getphone


      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:

    2x2 struct array with fields:
    2x2 struct array with fields:
    Result record 2:
    Mary Smith
    (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:
    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


Related Topics