Main Content


Class: matlab.coverage.Result
Package: matlab.coverage

Retrieve information from coverage results

Since R2023a



summary = coverageSummary(results,type) retrieves summary information about the specified coverage type from the coverage results.


[summary,description] = coverageSummary(results,type) also retrieves the description of the code coverage analysis.

Input Arguments

expand all

Results of the code coverage analysis, specified as a matlab.coverage.Result vector.

Coverage type to retrieve information about, specified as one of the values in this table.

ValueCoverage Type
"statement"Statement coverage
"function"Function coverage
"decision"Decision coverage
"condition"Condition coverage
"mcdc"Modified condition/decision coverage (MC/DC)

Retrieving information about decision coverage, condition coverage, and modified condition/decision coverage (MC/DC) requires a MATLAB® Test™ license. For more information about coverage types, see Types of Code Coverage for MATLAB Source Code (MATLAB Test).

Data Types: char | string

Output Arguments

expand all

Coverage summary, returned as an N-by-2 matrix, where N is the length of results. Each row in the matrix corresponds to a coverage result. The first value in the row is the number of executed outcomes for the specified coverage type, and the second value is the total number of executable outcomes for that type.

Description of the code coverage analysis, returned as a 1-by-N structure array, where N is the length of results. Each element of the array corresponds to a coverage result. Use this argument to access detailed information about the code coverage analysis. For example, you can access source code location information and an execution count for each outcome within the code.


expand all

Run a suite of tests and collect the code coverage result. Then, retrieve information about statement coverage from the result.

In a file named quadraticSolver.m in your current folder, create the quadraticSolver function. The function takes as inputs the coefficients of a quadratic polynomial and returns the roots of that polynomial. If the coefficients are specified as nonnumeric values, the function throws an error.

function roots = quadraticSolver(a,b,c)
% quadraticSolver returns solutions to the
% quadratic equation a*x^2 + b*x + c = 0.

if ~isa(a,"numeric") || ~isa(b,"numeric") || ~isa(c,"numeric")
    error("quadraticSolver:InputMustBeNumeric", ...
        "Coefficients must be numeric.")

roots(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);
roots(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a);


To test the quadraticSolver function, create the SolverTest class in a file named SolverTest.m in your current folder. Define three Test methods that test the function against real solutions, imaginary solutions, and nonnumeric inputs.

classdef SolverTest < matlab.unittest.TestCase
    methods (Test)
        function realSolution(testCase)
            actSolution = quadraticSolver(1,-3,2);
            expSolution = [2 1];
        function imaginarySolution(testCase)
            actSolution = quadraticSolver(1,2,10);
            expSolution = [-1+3i -1-3i];
        function nonnumericInput(testCase)
            testCase.verifyError(@()quadraticSolver(1,"-3",2), ...

Import the classes used in this example.

import matlab.unittest.plugins.CodeCoveragePlugin
import matlab.unittest.plugins.codecoverage.CoverageResult

Create a test suite from the SolverTest class.

suite = testsuite("SolverTest");

Create a test runner and customize it using a plugin that provides programmatic access to the code coverage information for the source code in the file quadraticSolver.m.

runner = testrunner("textoutput");
format = CoverageResult;
p = CodeCoveragePlugin.forFile("quadraticSolver.m",Producing=format);

Run the tests. After the test run, the Result property of format holds the coverage result. In this example, all the tests pass.;
Running SolverTest
Done SolverTest

Access the statement coverage summary. The returned vector indicates that all four statements in the source code were executed by the tests.

result = format.Result;
summary = coverageSummary(result,"statement")
summary = 1×2

     4     4

Use the description of the code coverage analysis to retrieve the execution count for each statement.

[~,description] = coverageSummary(result,"statement")
description = struct with fields:
    statement: [1×4 struct]

   3   1   2   2

Version History

Introduced in R2023a