Skip to Main Content Skip to Search
Product Documentation

Phonebook Example

Purpose

An example of how to process an MWStructArray as output from a generated component might be:

Object[] tmp = myComponent.myFunction(1, myArray); 
MWStructArray myStruct = (MWStructArray) tmp[0]; 

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

Procedure

  1. If you have not already done so, copy the files for this example as follows:

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

      matlabroot\toolbox\javabuilder\Examples\PhoneExample
      
    2. At the MATLAB command prompt, cd to the new PhoneExample subfolder in your work folder.

  2. If you have not already done so, set the environment variables that are required on a development machine. See Settings for Environment Variables (Development Machine).

  3. Write the makephone function as you would any MATLAB function.

    The following code defines the makephone function:

    function book = makephone(friends)
    %MAKEPHONE Add a structure to a phonebook structure
    %   BOOK = MAKEPHONE(FRIENDS) adds a field to its input structure.
    %   The new field EXTERNAL is based on the PHONE field of the original.
    %   This file is used as an example for MATLAB 
    %   Builder for Java.
    
    %   Copyright 2006-2011 The MathWorks, Inc.
    
    book = friends;
    for i = 1:numel(friends)
        numberStr = num2str(book(i).phone);
        book(i).external = ['(508) 555-' numberStr];
    end

    This code is already in your work folder in makephone.m.

  4. While in MATLAB, issue the following command to open the Deployment Tool window:

    deploytool

  5. You create a Java application by using the Deployment Tool GUI to build a Java class that wraps around your MATLAB code.

    To compile or build the Java application using the Deployment Tool, use the following information as you work through this example in Building the Java Component:

    Project Namephonebookdemo
    Class Namephonebook
    File to compilemakephone.m

  6. Write source code for an application that accesses the component.

    The sample application for this example is in matlabroot\toolbox\javabuilder\Examples\PhoneExample\
    PhoneDemoJavaApp\getphone.java
    .

    The program defines a structure array containing names and phone numbers, modifies it using a MATLAB function, and displays the resulting structure array.

    The program listing is shown here.

     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, as shown:

      thePhonebook = new phonebook();

    • Calls the makephone method to create a modified copy of the structure by adding an additional field, as shown:

      result = thePhonebook.makephone(1, friends);

    • Uses a try-catch block to catch and handle any exceptions.

  7. Compile the getphone application using javac. When entering this command, ensure there are no spaces between path names in the matlabroot argument. For example, there should be no space between javabuilder.jar; and .\distrib\phonebookdemo.jar in the following example. cd to your work folder. Ensure getphone.java is in your work folder

    • On Windows, execute this command:

      javac -classpath
        .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar;
        .\distrib\phonebookdemo.jar getphone.java
      
    • On UNIX, execute this command:

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

  8. Run the application.

    To run the getphone.class file, do one of the following:

    • On Windows, type:

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

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

      Note   You should be using the same version of Java that ships with MATLAB. To find out what version of Java MATLAB is running, enter the following MATLAB command:

      version -java

        Caution   MathWorks only supports the Sun JDK and JRE. A certain measure of cross-version compatibility resides in the Sun software and it may be possible to run MCR-based components with non-Sun JDK's under some circumstances—however, compatibility is not guaranteed.

      Note   If you are running on the Mac 64-bit platform, you must add the -d64 flag in the Java command. See Limitations and Restrictions for more specific information.

    The getphone program should display the 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
    

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS