Invalid default value for property error in class properties

20 visualizaciones (últimos 30 días)
NoYeah
NoYeah el 24 de Abr. de 2020
Comentada: NoYeah el 24 de Abr. de 2020
Greeting
I made a class and run dthe code in .m file
classdef myclass
properties (Constant)
a=physconst('LightSpeed');
b=a/2
end
end
it occurs error and says
Invalid default value for property 'b' in 'myclass':
Unrecognized function or variable 'a'.
physconst("LightSpeed") is a function in addon which name is 'Phased Array System Toolbox'
and its version is 4.3
a=physconst('LightSpeed')
b=a/2
I put thosdde two lines in command window
and it works fine...
so I have removed constant and tried to initialize them with class constructor
classdef myclass
properties
a;
b;
end
methods
function obj=myclass(obj)
a=physconst('LightSpeed');
b=a/2;
end
end
end
but the same error occured
I want to initialize my class property value with addon function but it have bring out error
How to deal with this error?

Respuesta aceptada

per isakson
per isakson el 24 de Abr. de 2020
Editada: per isakson el 24 de Abr. de 2020
In the properties block one must refer to a as MyClass.a when Constant.
>> mc = MyClass
mc =
MyClass with properties:
a: 299792458
b: 149896229
>> which physconst -all
C:\Program Files\MATLAB\R2018b\toolbox\shared\siglib\physconst.m
>>
where
classdef MyClass
properties (Constant)
a = physconst('LightSpeed');
b = MyClass.a/2;
end
end
In your second class definition the constructor
function obj=myclass(obj)
a=physconst('LightSpeed');
b=a/2;
end
shall be
function obj=myclass()
obj.a = physconst('LightSpeed');
obj.b = obj.a /2;
end

Más respuestas (0)

Categorías

Más información sobre Software Development Tools en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by