Coverage for MATLAB Function Blocks
This example explains how Model Coverage relates to MATLAB® code inside a MATLAB Function Block.
The slvnvdemo
_e
ml_model_coverage_demo
model contains several MATLAB Function Blocks that demonstrate model coverage behavior. The model is already configured to collect coverage. On the Simulation tab, click Run (Coverage).
load_system('slvnvdemo_eml_model_coverage_demo') sim('slvnvdemo_eml_model_coverage_demo')
When the simulation is complete, the Coverage Details pane opens on the right side of the Simulink window. In the Coverage Details, scroll up to see the Summary section of the report. In this section, you can see the top model and each MATLAB function block, which are named according to the type of MATLAB function block coverage they demonstrate.
Function Coverage
Click on the block titled Function coverage to show this block in the Coverage Details pane.
Functions in the MATLAB function block report whether each function within the block is executed at least once with a decision outcome. In the Function coverage block, 2 out of the 3 functions were called, resulting in 67% decision coverage for the block.
If-Statement Coverage
Click on the block titled If coverage.
If
statements in the MATLAB function block report decision coverage to indicate whether the if
statement is true or false. Full coverage requires at least one execution where the if
statement is true and at least one execution where it is false. Coverage requirements do not change when an else
statement is added.
In this MATLAB function block, there are 8 decision outcomes. 6 of the 8 are the true and false outcomes of the 3 if-statements, and the other 2 are the decision outcomes that indicate that each of the 2 functions are called.
And-Or Coverage
Click on the block titled and-or coverage.
If statements in the MATLAB function block that have &&
and ||
in their expressions receive condition and decision coverage. Full condition coverage requires that each condition evaluates true at least once and evaluates false at least once. Full MCDC coverage requires that each condition is shown to independently affect the decision (if-statement) outcome.
Switch-Case Coverage
Click on the block titled Switch-case coverage.
Switch-case statements in the MATLAB function block report decision outcomes that indicate which case statements execute during simulation. To receive full coverage, your model must execute each case statement at least once. Even if the otherwise keyword is not used, there must still be at least one execution of the switch statement where no case statements are valid to achieve full coverage.
While Coverage
Click on the block titled While coverage.
While statements in the MATLAB function block reports the while loop decision outcomes. Full coverage requires at least one execution where the while loop expression is true and at least one execution where the expression is false.
For coverage
Click on the block titled For Coverage.
For statements in the MATLAB function block report decision outcomes for the loop expression. Full coverage requires at least one execution where the loop expression is true and at least one execution where it is false.
Assignment Coverage
Click on the block titled Assignment Coverage.
Logical expressions in assignment statements within a MATLAB function block that contain &&
and ||
report condition coverage. Full condition coverage requires that each condition evaluates true at least once and false at least once. Full MCDC requires that each condition independently affects the parent decision outcome.
External MATLAB Files and Local or Nested Functions
External MATLAB files and local or nested functions called by a function inside a MATLAB function block receive coverage the same way that the rest of the code inside the block does. The difference is that external MATLAB files generate a separate cvdata
object, and therefore a separate code coverage report and are not included in the main model coverage report. The summary report contains a link to the code coverage report for each external MATLAB file.