Why do I get the error "Invalid default value for property" when trying to define one default instance variable value in terms of another?
32 views (last 30 days)
Show older comments
MathWorks Support Team
on 11 Sep 2019
Answered: MathWorks Support Team
on 27 Jan 2020
Why do I get the error "Invalid default value for property" when trying to define one default instance variable value in terms of another?
e.g.
classdef Example
properties
A = {'a','b','c'}
B = [A,'d']
end
end
gives me the following error when I try to instantiate an example object
>> Example
Invalid default value for property 'B' in class 'Example':
Undefined function or variable 'A'.
Accepted Answer
MathWorks Support Team
on 11 Sep 2019
It is easier to see the reason for this issue once you fully-qualify the name of an instance variable with which you are defining another.
e.g.
classdef Example
properties
A = {'a','b','c'}
B = [Example.A,'d']
end
end
yields the error
>> Example
Invalid default value for property 'B' in class 'Example':
The property 'A' in class 'Example' must be accessed from a class instance because it is not a Constant property.
Because 'B' is defined in terms of A, we must either define 'A' to be a "Constant" property or we must initialize B in the constructor.
i.e.
classdef Example
properties (Constant)
A = {'a','b','c'}
end
properties
B = [Example.A,'d']
end
end
or
classdef Example
properties
A = {'a','b','c'}
B
end
methods
function obj = Example()
obj.B = [obj.A,'d'];
end
end
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!