Main Content

na_0024: Shared data in MATLAB functions

Since R2020a

Guideline Publication

Control Algorithm Modeling Guidelines - Using MATLAB®, Simulink®, and Stateflow®

Sub ID Recommendations

  • NA-MAAB — a

  • JMAAB — a

MATLAB Versions

All

Rule

Sub ID a

Signal lines shall be used to connect data that is shared between MATLAB Function blocks.

Custom Parameter

Not Applicable

Example — Correct

function ErrorFlag = EngineFaultEvaluation(EngineData,ErrorFlag_In)
%#codegen
	RMP_HIGH = 10000;
	RMP_LOW = 10;
	HIGHRPMFAULT = 2^1;
	LOWRPMFAULT = 2^2;
	ErrorFlag = ErrorFlag_In;
	if EngineData > RPM_HIGH
		ErrorFlag = bitor(ErrorFlag,HIGHRPMFAULT);
	end
	if EngineData < RPM_LOW
		ErrorFlag = bitor(ErrorFlag,LOWRPMFAULT);
	end
	
function ErrorFlag = WheelFaultEvaluation(WheelData,ErrorFlag_In)
%#codegen
	SLIP_HIGH = 1000;
	WHEELSLIP = 2^3;
	ErrorFlag = ErrorFlag_In;
	if WheelData > SLIP_HIGH
		ErrorFlag = bitor(ErrorFlag,WHEELSLIP);
	end
end
	
Example — Incorrect

function EngineFaultEvaluation(EngineData)
%#codegen
	global ErrorFlag_DataStore
	RMP_HIGH = 10000;
	RMP_LOW = 10;
	HIGHRPMFAULT = 2^1;
	LOWRPMFAULT = 2^2;
	if EngineData > RPM_HIGH
		ErrorFlag_DataStore = bitor(ErrorFlag_DataStore,HIGHRPMFAULT);
	end
	if EngineData < RPM_LOW
		ErrorFlag_DataStore = bitor(ErrorFlag_DataStore,LOWRPMFAULT);
	end
	
function WheelFaultEvaluation(WheelData)
%#codegen
	global ErrorFlag_DataStore
	SLIP_HIGH = 1000;
	WHEELSLIP = 2^3;
	if WheelData > SLIP_HIGH
		ErrorFlag_DataStore = bitor(ErrorFlag_DataStore,WHEELSLIP);
	end
end
	

Rationale

Sub ID a:

  • When a data store is used, the readability of the data flow decreases and can lead to errors in the update reference timing.

Verification

Model Advisor check: Check MATLAB code for global variables (Simulink Check)

Last Changed

R2020a

Version History

Introduced in R2020a