In Create Custom Functionality Using MATLAB Function Block, you created an example model with a MATLAB Function block that calculates the mean and standard deviation for a set of input values. The software enables debugging for a MATLAB Function when you set a breakpoint.
To debug the MATLAB Function in this model:
and double-click the MATLAB Function block
open the editor.
len = length(vals);
A red dot appears in the line margin, indicating the breakpoint.
Simulate the model.
Simulation pauses when execution reaches the breakpoint. This is indicated by a green arrow in the margin.
The execution arrow advances to the next line of
which calls the local function
In the toolbar, click Step In.
Execution advances to enter the local function
avg. Once you are in a local
function, you can use the Step 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 to
execute the only line in the local function
When the local function
avg finishes executing,
a green arrow, pointing down, appears under the last line of the function.
Click Step to
return to the function
Execution advances to the line after the call to the local function
Click Step twice
to calculate the
stdev and to execute the
The plot function executes in MATLAB®:
In the MATLAB Function Block Editor, a green
arrow points down under the last line of code, indicating the completion
of the function
Click Continue to continue execution of the model.
The computed values of
in the Display blocks.
In the MATLAB Function Block Editor, click Quit Debugging to stop simulation.
To help you debug code, 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 beside the line of code and select Set Conditional Breakpoint. Type the condition in the pop-up window. You can use any valid MATLAB expression as a condition. This condition 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 also perform these actions from the Breakpoints menu.
While you simulate a MATLAB Function block, you can use several tools to keep track of variable values in the function.
In the MATLAB Function Block Editor, place the mouse cursor over the variable text and observe the pop-up display.
For example, to watch the variable
simulation, place the mouse cursor over the text
the code. The value of
len appears adjacent to
the cursor, as shown:
You can report the values for a function
variable with the Command Line Debugger utility in the MATLAB window
during simulation. When you reach a breakpoint, the Command Line Debugger
debug>>, appears. At this prompt,
you can see the value of a variable defined for the MATLAB
Function block by entering its name:
debug>> stdev 1.1180 debug>>
The Command Line Debugger also provides the following commands during simulation:
Quit debugging and terminate simulation.
Continue execution to next breakpoint.
Quit debugging and terminate 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
Equivalent to "print <var>" if variable is in the current scope.
Display the variables in the current scope.
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 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, for example,
evalin('base','whos'). To return to the MATLAB base workspace, use the
You can display the execution result of a MATLAB Function block line by omitting the terminating semicolon. If you do, execution results for the line are echoed to the MATLAB window during simulation.
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, set Simulation
range checking in the Diagnostics: Data Validity pane
of the Configuration Parameters dialog box to
To specify a range for input and output data, follow these steps:
In the Ports and Data Manager, select the input or output of interest.
The data properties dialog box opens.
In the data properties dialog box, select the General tab and enter a limit range, as described in Setting General Properties.
Use the following tools during a MATLAB Function block debugging session:
|Tool Button||Description||Shortcut Key|
Access this tool from the Editor tab by selecting Build Model > Build.
Check for errors and build a simulation application (if no errors are found) for the model containing this MATLAB Function block.
Access this tool from the Editor tab by selecting Build Model > Update Diagram.
Check for errors based on the latest changes you make to the MATLAB Function block.
Access this tool from the Editor tab by selecting Build Model > Update Ports.
Updates the ports of the MATLAB Function block with the latest changes made to the function argument and return values without closing the MATLAB Function Block Editor.
Start simulation of the model containing the MATLAB Function block. If execution is paused at a breakpoint, continues debugging.
Stop simulation of the model containing the MATLAB Function block. Alternatively, from the Editor tab, select Quit Debugging if execution is paused at a breakpoint.
Access this tool by selecting Breakpoints > Set/Clear.
Set a new breakpoint or clear an existing breakpoint for the selected line of code in the MATLAB Function block. The presence of the text cursor or highlighted text selects the line. A breakpoint indicator appears on the selected line.
click the hyphen character (
Access this tool by selecting Breakpoints > Enable/Disable.
Enable or disable an existing breakpoint for the selected line of code in the MATLAB Function block. If the breakpoint is disabled, an indicator appears on the selected line.
Access this tool by selecting Breakpoints > Set Condition.
Set a condition on the breakpoint for the selected line of code in the MATLAB Function block. If the breakpoint has a condition associated with it, an indicator appears on the selected line.
Access this tool by selecting Breakpoints > Clear All.
Clear all existing breakpoints in the MATLAB Function block code.
Step through the execution of the next line of code in the MATLAB Function block. 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.
Step through the execution of the next line of code in the MATLAB Function block. If the line calls a local function, step into the first line of the local function. You can use this tool only after execution has stopped at a breakpoint.
Step out of line-by-line execution of the current function or local function. If in a local function, the debugger continues to the line following the call to this local function. You can use this tool only after execution has stopped at a breakpoint.
Continue debugging after a pause, such as stopping at a breakpoint. You can use this tool only after execution has stopped at a breakpoint.
Exit debug mode. You can use this tool only after execution has stopped at a breakpoint.