Main Content

Debug MATLAB Function Blocks

You can use the MATLAB Function Block Editor to debug MATLAB Function block code. The MATLAB Function Block Editor includes some of the same debugging tools available in the MATLAB® editor. To debug MATLAB Function block code, set at least one breakpoint and run the simulation.

Debug an Example Function

This example uses the model call_stats_block2, which you create in Implement MATLAB Functions in Simulink with MATLAB Function Blocks. To debug the MATLAB Function block code in this model:

  1. Open the call_stats_block2 model. Double-click the MATLAB Function block to open the editor.

  2. In the MATLAB Function Block Editor, click the line number to the left of the line:

    len = length(vals);

    The line number is highlighted in red, which indicates the breakpoint.

    MATLAB function code with a breakpoint set on the line 7.

  3. Simulate the model.

    The simulation pauses when execution reaches the breakpoint. A green arrow points to where the simulation has paused.

    This image shows the debugging breakpoint and where the execution paused with the highlighted line.

  4. On the Function tab, click Step Over to advance execution.

    The execution advances to the next line of stats, which calls the local function avg.

  5. On the Function tab, click Step In.

    Execution advances to the local function avg. Once you are in a local function, you can use the Step Over or Step In commands to advance execution. If the local function calls another local function, use Step In to enter it. If you want to execute the remaining lines of the local function, use Step Out.

  6. Click Step Over to execute the only line in avg.

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

    Execution advances to the line after the call to avg.

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

    The plot function executes in MATLAB:

    This shows a plot of the input values and a line drawn through the values.

  9. Click Continue to continue executing the model.

    If you return to the model, the computed values of mean and stdev appear in the Display blocks.

  10. In the MATLAB Function Block Editor, click Stop to stop the simulation.

Note

You can debug MATLAB Function blocks in Normal and Accelerator modes. However, if you simulate a model in Accelerator mode that uses a MATLAB Function block in a model reference, the parent model simulation ignores the breakpoints.

Set Conditions on Breakpoints

You can enter a MATLAB expression as a condition on a breakpoint inside a MATLAB Function block. Simulation then pauses on that breakpoint only when the condition is true. To set a conditional breakpoint, in the MATLAB Function Block Editor, right-click the line number to the left of a line of code and select Set Conditional Breakpoint, and enter the condition in the dialog box. You can use any valid MATLAB expression as a condition. This expression can include numerical values and any data that is in scope at the breakpoint.

To add or modify a condition on an existing breakpoint, right-click the breakpoint and select Set/Modify Condition.

Watch Function Variables During Simulation

You can use several tools to track the variable values of the function while you simulate a MATLAB Function block.

Watch Function Variables in the Code

To view the value of a variable in the function of a MATLAB Function block during simulation in the code:

  1. Add a breakpoint to the MATLAB Function Block Editor.

  2. Run the model.

  3. When the simulation pauses, point to a variable name. A tooltip displays the current variable value.

    For example, to watch the variable stdev during simulation, point to len in the code.

    The cursor points to the variable len, and the value of the variable populates during simulation.

Watch Function Variables in the Symbols Pane

To view the value of a variable in the Symbols pane in a MATLAB Function block during simulation:

  1. Open the Symbols pane. In the Function tab, click Edit Data.

  2. Add a breakpoint to the MATLAB Function Block Editor.

  3. Run the model.

  4. When the simulation pauses, the Symbols pane updates with the values at the breakpoint. If the variable is an array, the Value column displays the dimension and data type of the variable. Point to the Value column to see the values.

    For example, to view the variable vals during simulation, point to the value of vals in the Value column.

    The cursor points to the variable vals, and the value of the variable populates during simulation.

Watching Function Variables with the Command Line Debugger

You can view the values for a function variable in the Command Window during simulation. When you reach a breakpoint, enter the name of a variable at the debug>> prompt to see its value.

debug>> stdev
 
	1.1180

debug>> 

You can also enter these commands in the command window while debugging:

CommandDescription

dbcont

Continue execution to next breakpoint.

dbquit

Quit debugging and terminate the simulation.

dbstep [in|out]

Advance to next program step after a breakpoint is encountered. Step over or step into/out of a MATLAB local function.

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 from the MATLAB base workspace, use load command after simulation completes.

<var>

Equivalent to print <var> if the 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.

If you enter other commands at the debug>> prompt, the results execute in the workspace of the MATLAB Function block. To issue a command in the MATLAB base workspace, use the evalin command with the first argument "base" followed by the second argument command, for example, evalin("base","whos"). You cannot define or change variables at the debug>> prompt.

Display Size Limits

The MATLAB Function Block Editor does not display the contents of matrices that have more than two dimensions or more than 200 elements. For matrices that exceed these limits, the MATLAB Function Block Editor displays the shape and base type only.

Check for Data Range Violations

MATLAB Function blocks check inputs and outputs for data range violations when the input or output values enter or leave the blocks. To enable data range violation checking, open the Configuration Parameters window, click Diagnostics > Data Validity, and set Simulation range checking to error or warning.

Specify a Data Range

To specify a range for input and output variables, in the MATLAB Function Block Editor:

  1. In the Symbols pane, click the variable of interest.

  2. In the Property Inspector, in the Properties tab, adjust the Limit range property by entering the desired values for Minimum and Maximum.

Debug MATLAB Function Blocks in Initialize Function and Terminate Function Blocks

When you use MATLAB Function blocks in Initialize Function or Terminate Function blocks, you cannot use the buttons in the Debug Code section to navigate through breakpoints. You must use the command line debugger or the line number context menu options.

To use the line number context menu, in the MATLAB Function Block Editor, add a breakpoint and run the simulation. During simulation, right-click any of the line numbers after the breakpoint and click Continue to Here.

this image shows the line number context menu that appears when you debug a MATLAB Function block. The additional debugging options include Step Over, Step In, Step Out, Continue, and Stop

See Also

Related Topics