Using Simulink Previous page   Next Page

Debugging the Function in Simulation

You can debug your Embedded MATLAB Function block just like you can debug a function in MATLAB. In simulation, you test your Embedded MATLAB functions for run-time errors with tools similar to the MATLAB debugging tools.

When you start simulation of your model, Simulink checks to see if the Embedded MATLAB Function block has been built since creation, or since a change has been made to the block. If not, it performs the build described in Checking the Function for Errors. If no diagnostic errors are found, Simulink begins the simulation of your model.

Use the following procedure to debug the stats Embedded MATLAB function during simulation of the model:

  1. If not already open, open the call_stats_block2 model that you save at the end of Programming the Embedded MATLAB Function, and double-click its Embedded MATLAB Function block stats to open it for editing in the Embedded MATLAB Editor.
  2. 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. You can also use the Set/Clear Breakpoint tool to insert the breakpoint on the line where the cursor is positioned.

  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. In the Embedded MATLAB Editor window, click the Step tool to advance execution one line to line 7.
  1. The execution arrow advances to line 7 of stats.

    You can also step execution by entering dbstep at the Command Line Debugger. See Watching with the Command Line Debugger for a description of the Command Line Debugger in MATLAB.

    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. 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 a subfunction, you can use the Step or Step In tool to advance execution. If the subfunction calls another subfunction, use the Step In tool to step into it. If you want to execute the remaining lines of the subfunction, 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 Step twice to execute line 8 and the plot function in line 9.
  1. The plot function executes in MATLAB, and you see the following plot.

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

  1. Click the Continue tool

    to continue execution of the model.
  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.

    You can also continue execution by entering dbcont at the Command Line Debugger. See Watching with the Command Line Debugger for a description of the Command Line Debugger in MATLAB.

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

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

Previous page  Debugging an Embedded MATLAB Function Watching Function Variables During Simulation Next page

© 1994-2005 The MathWorks, Inc.