Debugging an Embedded MATLAB Function

Checking Embedded MATLAB Functions for Syntax Errors

Before you can build a simulation application for a model, you must fix syntax errors. Follow these steps to check the Embedded MATLAB function meanstats for syntax violations:

  1. Open the Embedded MATLAB function meanstats inside the Stateflow chart in the call_stats_function_stateflow model that you updated in Programming an Embedded MATLAB Function.

    The Embedded MATLAB Editor uses the MATLAB M-Lint Code Analyzer to automatically check your function code for errors and recommend corrections (see Using M-Lint with Embedded MATLAB Code in the Embedded MATLAB User's Guide).

  2. In the Embedded MATLAB Editor, select the Build tool to build a simulation application for the example Simulink model.

    If there are no errors or warnings, the Builder window appears and reports success. Otherwise, it lists errors. For example, if you change the name of subfunction avg to a nonexistent subfunction aug in meanstats, the Builder reports these errors:

    Each error message appears with a red button. The selected error message displays diagnostic information in the bottom pane.

  3. Click the link in the diagnostic message to display the offending line of code, as shown.

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 the Stateflow Debugger. When you simulate your model, your Embedded MATLAB functions undergo diagnostic tests for missing or undefined information and possible logical conflicts as described in Checking Embedded MATLAB Functions for Syntax Errors. If no errors are found, the simulation of your model begins.

Follow these steps to simulate and debug the meanstats Embedded MATLAB function during run-time conditions:

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

    A small red ball appears next to line 6, indicating that you set a breakpoint.

  2. Click the Start Simulation icon to begin simulating the model.

    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 pause is indicated by a small green arrow in the left margin as shown.

  3. Click the Step icon to advance execution one line to line 7.

    Notice that line 7 calls the subfunction avg. If you click Step here, execution advances to line 9, past the execution of the subfunction avg. In order to track execution of the lines in the subfunction avg, you must click the Step In icon.

  4. Click the Step In icon to advance execution to the first line of the called subfunction avg.

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

  5. Click the Step icon to execute the only line in the subfunction avg.

    When the subfunction avg finishes its execution, you see a green arrow pointing down under its last line.

  6. Click the Step icon to return to the function meanstats.

    Execution advances to the line after to the call to the subfunction avg, line 9.

  7. To display the value of the variable len, place your pointer over the text len in line 6 for at least a second.

    The value of len appears adjacent to your pointer.

    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 your pointer 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 function data in the MATLAB Command Window during simulation. When you reach a breakpoint, the debug>> command prompt appears in the MATLAB Command Window (you might have to press Enter to see it). At this prompt, you can inspect data defined for the Embedded MATLAB function by entering the name of the data, as shown in this example:

    debug>> len
    len =
         4
    debug>>
    

    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 Command Window during simulation.

  8. Click the Continue icon to leave the function until it is called again and the breakpoint on line 6 is reached.

    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 icon accomplishes the same thing.

  9. Click the breakpoint at line 6 to remove it and click the green arrow to complete the simulation.

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

Checking for Data Range Violations

During debugging, Embedded MATLAB functions automatically check input and output data for data range violations.

Specifying a Range

To specify a range for input and output data, follow these steps:

  1. In the Model Explorer, select the Embedded MATLAB function input or output of interest.

    The Data properties dialog box opens in the Dialog pane of the Model Explorer.

  2. In the Data properties dialog box, select the Value Attributes tab and enter a limit range, as described in Limit range properties.

Controlling Data Range Checking

To control data range checking, follow these steps:

  1. Open the Stateflow Debugger, as described in Opening the Stateflow Debugger.

  2. In the Error checking options pane, perform one of these actions:

    To:Do This:
    Enable data range checking

    Select the Data Range check box

    Disable data range checking

    Clear the Data Range check box

  


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