Resolve Error: Nontunable Property Cannot Be Defined by Subscripted Assignment
Issue
If a nontunable property of a System object™ is a structure, you must completely assign the structure when you define it. If you use subscripting to perform partial assignment of a structure in a System object property, you see this error during code generation:
The
nontunable property 'myProperty' of class
'MySystemObject' cannot be defined by subscripted
assignment. Use complete assignment instead.
Possible Solutions
Consider the System object
MySystemObject, which has a single nontunable property. The function
useMyObj_error uses the System object. The function creates an instance of MySystemObject. It
implicitly defines nonTunableProperty as a structure by assigning
values to nonTunableProperty.fieldA and
nonTunableProperty.fieldB. Because the function defines
nonTunableProperty as a structure and it does not assign values
to all fields of the structure simultaneously, code
generation fails for
useMyObj_error.
classdef MySystemObject < matlab.System properties(Nontunable) nonTunableProperty end methods(Access = protected) function stepImpl(obj) disp(obj.nonTunableProperty); end end end function useMyObj_error mySystemObject = MySystemObject; mySystemObject.nonTunableProperty.fieldA = "a"; mySystemObject.nonTunableProperty.fieldB = "b"; mySystemObject(); end
To resolve this error, assign values to all fields of the structure when you define it. For example, code generation succeeds for this function.
function useMyObj mySystemObject = MySystemObject; mySystemObject.nonTunableProperty = struct("fieldA","a","fieldB","b"); mySystemObject(); end