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.
This example uses the model
call_stats_block2, which you create in
Create Custom Functionality Using MATLAB Function Blocks. To debug the
MATLAB Function block code in this model:
call_stats_block2 model. Double-click
the MATLAB Function block to open the editor.
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.
Simulate the model.
The simulation pauses when execution reaches the breakpoint. A green arrow points to where the simulation has paused.
On the Function tab, click Step Over to advance execution.
The execution advances to the next line of
stats, which calls the
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.
Click Step Over to execute the only line in
Click Step Over to return to the function
Execution advances to the line after the call to
Click Step Over twice to calculate the
stdev and to execute the
plot function executes in MATLAB:
Click Continue to continue executing the model.
If you return to the model, the computed values of
stdev appear in the Display blocks.
In the MATLAB Function Block Editor, click Stop to stop the simulation.
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.
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.
You can use several tools to track the variable values of the function while you simulate a MATLAB Function block.
To display the value of a variable in the function of a MATLAB Function block during simulation:
Add a breakpoint to the MATLAB Function Block Editor. Run the model.
When the simulation pauses, point to a variable name. A tooltip displays the current variable value.
For example, to watch the variable
len during simulation,
len in the code.
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:
Continue execution to next breakpoint.
Quit debugging and terminate the simulation.
Advance to next program step after a breakpoint is encountered. Step over or step into/out of a MATLAB local function.
Display help for command line debugging.
Display the value of the variable
Saves all variables in the current scope to the specified file. Follows
the syntax of the MATLAB
Display the variables in the current scope.
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,
return to the MATLAB base workspace, use the
The MATLAB Function Block Editor does not display the contents of
matrices that have more than two dimensions or more than
For matrices that exceed these limits, the MATLAB Function Block Editor
displays the shape and base type only.
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
To specify a range for input and output data, open the MATLAB Function Block Editor, click Ports & Data Manager, and select the input or output of interest. In the General tab, adjust the Limit range property by entering the desired values for Minimum and Maximum.