defining properties in a class
Afficher commentaires plus anciens
I have the following class
classdef classname
properties (Constant)
Number=1
end
end
I can change the value of `Number`
classname.Number=2
How can I defined properties so that one cannot change the value of the variables defined in `properties`?
Réponses (1)
This doc page seems to suggest this is not natively possible. However, you could implement it as method:
%(untested code)
classdef classname
properties (Private)
%use a struct to store the defaults to keep all your static
%properties in one place
static.Number=1;
end
methods
function val=Number(obj)
val=obj.static.Number;
end
end
end
13 commentaires
Walter Roberson
le 3 Août 2021
Modifié(e) : Walter Roberson
le 3 Août 2021
I disagree that that link says it is not natively possible; the link says specifically,
"In MATLAB, classes can define constant properties, but not "static" properties in the sense of other languages like C++. You cannot change constant properties from the initial value specified in the class definition."
So classname.Number=2 would be expected to be an error after the initialization.
That was my expectation as well, but given that OP implied this was allowed, I assumed this was the correct interpretation.
But you are correct: this already results in an error (while my code doesn't).
% classdef classname
% properties (Constant)
% Number=1
% end
% end
a=classname;
a.Number=2
Deepa Maheshvare
le 3 Août 2021
Modifié(e) : Deepa Maheshvare
le 3 Août 2021
Rik
le 3 Août 2021
classname isn't an object of your class anymore, it has become a struct.
Walter Roberson
le 3 Août 2021
Re-read what Rik pointed to, https://www.mathworks.com/help/matlab/matlab_oop/matlab-vs-other-oo-languages.html#buwskub
in which the description specifically says that
A.B = C
will replace A with a struct in the case that A is a class name .
It does not do that if A is an object of a class, only when A is the name of a class.
It is a bit strang that you normally get a warning when converting a class to a struct, but not with this syntax. Maybe worth an enhancement request.
classname.Number=2 % no warning
struct(line)
Deepa Maheshvare
le 3 Août 2021
Modifié(e) : Deepa Maheshvare
le 3 Août 2021
If your value isn't the same for every instance of your class, doesn't it make more sense to use something similar to what I suggested, but use a persistent variable in a method to store a specific value?
It is a bit confusing to me what your end goal is with what you're doing.
Your edit did not clear up the confusion for me.
Also, you should not leave comments if they are no longer true, they will only serve to confuse.
Deepa Maheshvare
le 3 Août 2021
Rik
le 3 Août 2021
That sounds like you need a normal property, not a constant.
Deepa Maheshvare
le 3 Août 2021
Why would it? This is what it returns with your classdef.
I don't understand why you instist on calling things static when they aren't.
main(10)
function main(arg)
classname.fun1(arg,arg)
end
Deepa Maheshvare
le 3 Août 2021
Catégories
En savoir plus sur Argument Definitions dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!