Stateflow User's Guide Previous page   Next Page

Run-Time Debugging for Embedded MATLAB Functions

You use simulation to test your Embedded MATLAB functions for run-time errors that are not detectable by Stateflow diagnostics. When you start simulation of your model, Simulink performs diagnostic tests of your completed Embedded MATLAB functions for missing or undefined information and possible logical conflicts as described in Checking Embedded MATLAB Functions for Syntax Errors. If no errors are found, Stateflow begins the simulation of your model.

Use the following procedure to simulate and debug the stats Embedded MATLAB function during run-time conditions:

  1. In the Embedded MATLAB Editor, in the left margin of line 6, click the dash (-) character.

  1. A small red ball appears in the margin of line 6 indicating that you have set a breakpoint.

  1. Click the Start Simulation tool to begin simulating the model.
  1. If you get any errors or warnings, make corrections before you try to simulate again. Otherwise, simulation pauses when execution reaches the breakpoint you set. This is indicated by a small green arrow in the left margin as shown.

  1. Click the Step tool to advance execution one line to line 7.
  1. Notice that line 7 calls the subfunction avg. If you click Step here, execution advances to line 8, past the execution of the subfunction avg. In order to track execution of the lines in the subfunction avg, you need to click the Step In tool.

  1. Click the Step In tool to advance execution to the first line of the called subfunction avg as shown.

  1. Once you are in the subfunction, you can advance through the subfunction one line at a time with the Step tool. If the subfunction calls another subfunction, use the Step In tool to step into it. If you want to continue through the remaining lines of the subfunction and go back to the line after the subfunction call, click the Step Out tool .

  1. Click the Step tool to execute the only line in the subfunction avg.
  1. The subfunction avg finishes its execution, and you see a green arrow pointing down under its last line as shown.

  1. Click the Step tool to return to the function stats.

  1. Execution advances to the line after to the call to the subfunction avg, line 8.

  1. Click the Step tool twice to execute line 8 and the plot function in MATLAB in line 9.
  1. The plot function executes in MATLAB, and you see the following plot.

    In the Embedded MATLAB Editor, a green arrow pointing down under line 9, indicating the completion of the function stats.

  1. To display the value of the variable len, place the mouse cursor over the text len in line 6 for at least a second.
  1. The value of len appears adjacent to the cursor as shown:

    You can display the value for any data in the Embedded MATLAB block function in this way, no matter where it appears in the function. For example, you can display the values for the vector vals by placing the cursor over it as an argument to the function length in line 6, or as an argument in the function header.

    You can also report the values for Embedded MATLAB block function data in the MATLAB window during simulation. When you reach a breakpoint, the DB>> command prompt appears in the MATLAB window (you might have to press Enter to see it). At this prompt you can inspect data defined for the Embedded MATLAB block by entering the name of the data as shown in the following example:

    As another debugging alternative, you can display the execution result of an Embedded MATLAB function line by omitting the terminating semicolon. If you do, execution results for the line are echoed to the MATLAB window during simulation.

  1. Click the Continue tool to leave the function until it is called again and the breakpoint on line 6 is reached.
  1. At any point in a function, you can advance through the execution of the remaining lines of the function with the Continue tool. If you are at the end of the function, clicking the Step tool accomplishes the same thing.

    In the Simulink window, the computed values of mean and stdev now appear in the Display blocks.

  1. In the Embedded MATLAB Editor, select the Exit Debug Mode tool to stop simulation.

Previous page  Checking Embedded MATLAB Functions for Syntax Errors Model Coverage for an Embedded MATLAB Function Next page

© 1994-2005 The MathWorks, Inc.