Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Debugging an Embedded MATLAB Function Block

How Debugging Affects Simulation Speed

Debugging an Embedded MATLAB function slows simulation speed. If your model has many Embedded MATLAB Function blocks and debugging is enabled, the simulation speed is much slower than when debugging is disabled. For maximum simulation speed, disable debugging as described in Enabling and Disabling Debugging.

Enabling and Disabling Debugging

There are two levels of debugging available when using Embedded MATLAB Function blocks, model level debugging and block level debugging.

Debugging is enabled for all Embedded MATLAB functions by default, except for Embedded MATLAB functions in Stateflow. Debugging for Embedded MATLAB functions in Stateflow is controlled in the Simulation Target pane in the Configuration Parameters dialog.

Disable debugging for an entire model by clearing the Enable debugging/animation check box in the Simulation Target pane in the Configuration Parameters dialog. Disable debugging for an individual Embedded MATLAB Function block by clicking Enable Debugging in the Embedded MATLAB Editor Debug menu. If Enable Debugging is unavailable, then the Simulation Target pane in the Configuration Parameters dialog is controlling debugging.

Debugging the Function in Simulation

In Creating an Example Embedded MATLAB Function, you created an example model with an Embedded MATLAB Function block. You use this block to specify an Embedded MATLAB function stats that calculates the mean and standard deviation for a set of input values. In this section, you debug stats in the example model.

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 runtime errors with tools similar to the MATLAB debugging tools. See Watching with the Command Line Debugger and Debugging Tools for more information.

When you start simulation of your model, the Simulink software 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 Building the Function and Checking for Errors. If no diagnostic errors are found, the simulation of your model begins.

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

  1. Open the call_stats_block2 model that you saved at the end of Programming the Embedded MATLAB Function. Double-click its Embedded MATLAB Function block stats to open it for editing in the Embedded MATLAB Editor.

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

    len = length(vals);

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

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

  4. Click the Step icon to advance execution:

    The execution arrow advances to the next line of stats. Notice that this line calls the subfunction avg. If you click Step here, execution advances to the next line, past the execution of the subfunction avg. To track execution of the lines in the subfunction avg, you need to click the Step In icon.

  5. Click the Step In icon .

    Execution advances to enter the subfunction avg:

    Once you are in a subfunction, you can use the Step or Step In icons to advance execution. If the subfunction calls another subfunction, use the Step In icon to enter it. If you want to execute the remaining lines of the subfunction, click the Step Out icon .

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

    The subfunction avg finishes its execution, and you see a green arrow pointing down under its last line as shown.

  7. Click the Step icon to return to the function stats.

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

  8. Click Step twice to calculate the stdev and to execute the plot function.

    The plot function executes in MATLAB, and you see the following plot.

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

  9. Click the Continue Debugging icon to continue execution of the model.

    At any point in a function, you can advance through the execution of the remaining lines of the function with the Continue Debugging icon. If you are at the end of the function, clicking the Step icon accomplishes the same thing.

    The computed values of mean and stdev now appear in the Display blocks.

  10. In the Embedded MATLAB Editor, click the Exit Debug Mode icon to stop simulation.

Watching Function Variables During Simulation

While you are simulating the function of an Embedded MATLAB Function block, you can use several tools to keep track of variable values in the function. These tools are described in the topics that follow.

Watching with the Interactive Display

To display the value of a variable in the function of an Embedded MATLAB Function block during simulation, in the Embedded MATLAB Editor, place the mouse cursor over the variable text and observe the pop-up display.

For example, to watch the variable len during simulation, place the mouse cursor over the text len in the code. The value of len appears adjacent to the cursor, as shown:

You can display the value for any variable in the Embedded MATLAB function in this way, no matter where it appears in the function.

Watching with the Command Line Debugger

You can report the values for an Embedded MATLAB function variable with the Command Line Debugger utility in the MATLAB window during simulation. When you reach a breakpoint, the Command Line Debugger prompt, debug>>, appears. At this prompt, you can see the value of a variable defined for the Embedded MATLAB Function block by entering its name:

debug>> stdev
 
	1.1180

debug>> 

The Command Line Debugger also provides the following commands during simulation:

CommandDescription

ctrl-c

Quit debugging and terminate simulation.

dbcont

Continue execution to next breakpoint.

dbquit

Quit debugging and terminate simulation.

dbstep [in|out]

Advance to next program step after a breakpoint is encountered. Step over or step into/out of an Embedded MATLAB subfunction.

help

Display help for command line debugging.

print <var>

Display the value of the variable var in the current scope. If var is a vector or matrix, you can also index into var. For example, var(1,2).

save

Saves all variables in the current scope to the specified file. Follows the syntax of the MATLAB save command. To retrieve variables to the MATLAB base workspace, use load command after simulation has been ended.

<var>

Equivalent to "print <var>" if variable is in the current scope.

who

Display the variables in the current scope.

whos

Display the size and class (type) of all variables in the current scope.

You can issue any other MATLAB command at the debug>> prompt, but the results are executed in the workspace of the Embedded MATLAB Function block. To issue a command in the MATLAB base workspace at the debug>> prompt, use the evalin command with the first argument 'base' followed by the second argument command string, for example, evalin('base','whos'). To return to the MATLAB base workspace, use the dbquit command.

Watching with MATLAB

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.

Checking for Data Range Violations

When you enable debugging, Embedded MATLAB Function blocks automatically check input and output data for data range violations when the values enter or leave the blocks.

Specifying a Range

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

  1. In the Model Explorer, select the input or output of interest in the Embedded MATLAB Function block.

    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 Setting Value Attributes Properties .

Debugging Tools

Use the following tools during an Embedded MATLAB function debugging session:

Tool ButtonDescriptionShortcut Key

Build

Check for errors and build a simulation application (if no errors are found) for the model containing this Embedded MATLAB function.

Alternatively, from the Tools menu, select Build.

Ctrl+B

Start Simulation

Start simulation of the model containing the Embedded MATLAB function.

F5

Stop Simulation

Stop simulation of the model containing the Embedded MATLAB function. Alternatively, from the Debug menu, select Exit debug mode if execution is paused at a breakpoint.

Shift+F5

Set/Clear Breakpoint

Set a new breakpoint or clear an existing breakpoint for the selected Embedded MATLAB code line. The presence of the text cursor or highlighted text selects the line. A Breakpoint Indicator appears in the breakpoints column for the selected line. Alternatively, click the hyphen character (-) in the breakpoints column for the line. A breakpoint indicator appears in place of the hyphen. Click the breakpoint indicator to clear the breakpoint.

F12

Clear All Breakpoints

Clear all existing breakpoints in the Embedded MATLAB function.

None

Step

Step through the execution of the next Embedded MATLAB code line. This tool steps past function calls and does not enter called functions for line-by-line execution. You can use this tool only after execution has stopped at a breakpoint. Alternatively, from the Debug menu, select Step.

F10

Step In

Step through the execution of the next Embedded MATLAB code line. If the line calls a subfunction, step into the first line of the subfunction. You can use this tool only after execution has stopped at a breakpoint. Alternatively, from the Debug menu, select Step In.

F11

Step Out

Step out of line-by-line execution of the current function or subfunction. If in a subfunction, the debugger continues to the line following the call to this subfunction. You can use this tool only after execution has stopped at a breakpoint. Alternatively, from the Debug menu, select Step Out.

Shift+F11

Continue Debugging

Continue debugging after a pause, such as stopping at a breakpoint. You can use this tool only after execution has stopped at a breakpoint. Alternatively, from the Debug menu, select Continue.

F5

Exit Debug Mode

Exit debug mode. You can use this tool only after execution has stopped at a breakpoint. Alternatively, from the Debug menu, select Exit Debug Mode.

Shift+F5

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

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