na_0022: Recommended patterns for Switch/Case statements

ID: Title

na_0022: Recommended patterns for Switch/Case statements

Priority

Mandatory

Scope

NA-MAAB

MATLAB Versions

All

Prerequisites

None

Description

Switch / Case statements must use constant values for the Case arguments. Input variables cannot be used in the Case arguments.

Correct:

function outVar = NA_0022_Pass(SwitchVar)
%# codegen
	switch SwitchVar
		case Case_1_Parameter % Parameter
			outVar = 0;
		case NA_0022.Case % Enumerated Data type
			outVar = 1;
		case 3 % Hard Code Value
			outVar = 2;
		otherwise
			outVar = 10;
	end
end

Incorrect:

function outVar = NA_0022_Fail(Case_1, Case_2, Case_3, SwitchVar)
%# codegen
	switch SwitchVar
		case Case_1
			outVar = 1;
		case Case_2
			outVar = 2;
		case Case_3 
			outVar = 3;
		otherwise
			outVar = 10;
		end
	end

Rationale

  • Verification and Validation

  • Code Generation

  • Simulation

See Also

  • jh_0026: Switch / Case statement

Last Changed

V3.0

Was this topic helpful?