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.

Files

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

Procedure

  1. 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.

  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];
    end

    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 compiler.build.javaPackage using the following information:

    FieldValue
    Library Namephonebookdemo
    Class Namephonebook
    File to Compilemakephone.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.

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

    The sample application for this example is in PhoneExample\PhoneDemoJavaApp\getphone.java.

     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.

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

  6. 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'))
  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 getphone.java
    • On UNIX®, type:

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

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

  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

      Note

      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

|

Related Topics