Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Test Client Data Integration Against MATLAB

This example shows how to test the integration between a Java® client and the addmatrix MATLAB® function. Using the Production Server Compiler app, you start a testing session and inspect the values passed between the client and the MATLAB function. You also set breakpoints in the MATLAB function and inspect the data using the MATLAB debugger.

  1. In MATLAB, enter the function to deploy on MATLAB Production Server™.

    function a = addmatrix(a1, a2)
    
    a = a1 + a2;
  2. In a text editor, paste the following Java client code.

    import java.net.URL;
    import java.io.IOException;
    import com.mathworks.mps.client.MWClient;
    import com.mathworks.mps.client.MWHttpClient;
    import com.mathworks.mps.client.MATLABException;
    
    interface MATLABAddMatrix 
      {
        double[][] addmatrix(double[][] a1, double[][] a2)
             throws MATLABException, IOException;
      }
    
    public class MPSClientExample {
        
        public static void main(String[] args){
        
            double[][] a1={{1,2,3},{3,2,1}};
            double[][] a2={{4,5,6},{6,5,4}};
            
            MWClient client = new MWHttpClient();
            
            try{
                MATLABAddMatrix m = client.createProxy(new URL("http://localhost:9910/addmatrix"),
                                                    MATLABAddMatrix.class);
                double[][] result = m.addmatrix(a1,a2);
                
                // Print the magic square
    
                printResult(result);
                
            }catch(MATLABException ex){
            
                // This exception represents errors in MATLAB 
                   System.out.println(ex);            
            }catch(IOException ex){
            
                // This exception represents network issues. 
                   System.out.println(ex);                    
            }finally{
            
                client.close();        
            }
        }
        
        private static void printResult(double[][] result){
            for(double[] row : result){
                for(double element : row){
                    System.out.print(element + " ");
                }
                System.out.println();
            }        
        }
    }
  3. Save the file as MPSClientExample.java.

  4. At the system command prompt, compile the Java client using the javac command.

    javac -classpath "matlabroot\toolbox\compiler_sdk\mps_clients\java\mps_client.jar" MPSClientExample.java
    
  5. In MATLAB, open the Production Server Compiler app.

    1. On the toolstrip, select the Apps tab.

    2. Click the arrow on the far right of the tab to open the apps gallery.

    3. Click Production Server Compiler.

  6. In the Type section of the toolstrip, select Deployable Archive from the list.

  7. Specify the MATLAB functions to deploy.

    1. In the Exported Functions section of the toolstrip, click the plus button.

    2. Using the file explorer, locate and select the addmatrix.m file.

      addmatrix.m is located in matlabroot\extern\examples\compiler.

    3. Click Open to select the file and close the file explorer.

      addmatrix.m is added to the field.

  8. Click Test Client.

  9. Check the value of the Port field.

    It must be:

    • an available port

    • the same port number the client is using

    For this example, the client expects use the port 9910. If the port is not available, you will need to update the client and recompile it.

  10. Click Start.

  11. At the system command prompt, run the Java client.

    java -classpath .;"matlabroot\toolbox\compiler_sdk\mps_clients\java\mps_client.jar" MPSClientExample
    

    Note

    You cannot run the Java client from the MATLAB command prompt.

    The application returns the following at the console:

    5.0 7.0 9.0
    9.0 7.0 5.0

    The Server Requests section of the app shows that the request completed successfully.

  12. Click the completed message in the app to see the values exchanged between the client and MATLAB.

  13. Click Input to view the arrays passed into MATLAB.

  14. Click Output to view the array returned to the client.

  15. Click Breakpoints > Break on MATLAB function entry.

  16. At the system command prompt, run the Java client.

    java -classpath .;"matlabroot\toolbox\compiler_sdk\mps_client\java\mps_client.jar" MPSClientExample
    

    Note

    You cannot run the Java client from the MATLAB command prompt.

  17. When the MATLAB editor opens, note that a breakpoint is set at the first line in the function and that processing has paused at the breakpoint.

    You now can use all of the MATLAB debugging tools to step through your function.

    Note

    You can create a timeout error in the client if you take a long time stepping through the MATLAB function.

  18. Switch to the main MATLAB window.

  19. Note that variables a1 and a2 are displayed in the MATLAB workspace.

  20. In the MATLAB editor, click Continue.

    The application returns the following at the console:

    5.0 7.0 9.0
    9.0 7.0 5.0

    The Server Requests section of the app shows that the request completed successfully.

  21. Click Stop to shutdown the test server.

  22. Click Close Test.

Related Topics

Was this topic helpful?