Ownership structure

IDFCNS-10
TitleOwnership of a structure resides with the creator of that structure.
PriorityStrongly recommended
Severity level4
DescriptionOwnership of a structure resides with the creator of that structure, so do not add or remove fields from an existing structure outside of the function in which it was created.
RationaleAdding or removing fields from an existing structure outside of the function in which it was created reduces the robustness of the code. Related to coder compatibility, in generated code, adding a new field to a structure that has already been read or indexed will cause an error.

Avoid:

function [out] = calculateSpeed(car)

out = car;

% do not remove fields from existing struct
car = rmfield(car, 'speed');

Instead use:

function [out] = calculateSpeed(car)

out = car;

% clear the field
car.speed = [];