Main Content

MATLAB Decision Count

Metric ID

slcomp.MATLABDecisions

Description

Use this metric to count the number of decisions in MATLAB® code associated with a unit or component.

This metric analyzes:

  • MATLAB functions in Simulink®, including MATLAB Function blocks and Interpreted MATLAB Function blocks

  • MATLAB functions in Stateflow® objects

  • Functions and methods in MATLAB files

Decision Counts for Common MATLAB Keywords

The following table shows how the metric calculates the decision count for common MATLAB keywords. The metric uses the cyclomatic complexity value from the checkcode function. For more information on the cyclomatic complexity value, see Measure Code Complexity Using Cyclomatic Complexity.

 Decision Counts Table

Computation Details

The metric:

  • Calculates the number of decisions in MATLAB code by taking the cyclomatic complexity calculated by the Code Analyzer and subtracting one to exclude the default path. For more information, see checkcode and Measure Code Complexity Using Cyclomatic Complexity.

  • Counts decisions from if, elseif, while, for, parfor, try, and case statements.

  • Ignores else, otherwise, and catch statements because they form the default path.

  • Does not compile the model. The metric only considers static information.

  • Only analyzes MATLAB code that does not contain syntax errors.

Collection

To collect data for this metric:

  • In the Model Maintainability Dashboard, in the Design Cyclomatic Complexity Breakdown section, click the Run metrics for widget icon. The distribution of the decisions appears in the MATLAB row and Distribution column. To view a table that shows the MATLAB decision count for each model component, click one of the bins in the distribution.

  • Use getMetrics with the metric identifier slcomp.MATLABDecisions.

Results

For this metric, instances of metric.Result return Value as the number of MATLAB decisions in the MATLAB code associated with each layer of a unit or component.

Examples

Suppose you have a unit that contains only a MATLAB Function block with this code:

function y = fcn(u)
    if u < 0
        % one decision
        y = -1*u;
    else
        % default path
        % zero decisions
        y = u;
    end
end

For an if-else statement, the number of decisions is one because the if statement represents one decision and the else statement represents the default behavior. The execution path follows the default path if no decisions are made.

  • If the input to the MATLAB Function block is less than zero, the output of the MATLAB Function block is the input multiplied by negative one.

  • Otherwise, by default, the output of the MATLAB Function block is equal to the input of the MATLAB Function block.

In this example, the number of MATLAB decisions is one.

Suppose the code includes elseif statements:

function y = fcn(u)
    if u < 0
        % one decision
        y = -1*u;
    elseif u == 1
        % one decision
        y = 1;
    elseif u == 2
        % one decision
        y = 2;
    else
        % default path
        % zero decisions
        y = u;
    end
end
The number of MATLAB decisions increases by one for each additional elseif statement in the code. MATLAB code that contains one if statement, two elseif statements, and one else statement contains three decisions. The else statement does not contribute to the number of decisions because the else statement is part of the default path. The default path is not included in the number of decisions because no decision is made to reach a default state.

See Also

| | |

Related Topics