defining properties in a class

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)

Rik
Rik le 3 Août 2021
Modifié(e) : Rik le 3 Août 2021

0 votes

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
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
Unable to set the 'Number' property of class ''classname'' because it is read-only.
Deepa Maheshvare
Deepa Maheshvare le 3 Août 2021
Modifié(e) : Deepa Maheshvare le 3 Août 2021
a=classname;
a.Number=2
Results in same error that you see
Unable to set the 'Number' property of class ''classname'' because it is read-only.
I actually did
>> classname.Number
ans =
1
>> classname.Number =2
classname =
struct with fields:
Number: 2
>> classname.Number
ans =
2
I'm doing something that is terribly wrong here. classname.Number=2 detelets all the other properties defined in classname and this doesn't return an error.
Rik
Rik le 3 Août 2021
classname isn't an object of your class anymore, it has become a struct.
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
classname = struct with fields:
Number: 2
struct(line)
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
Warning: The EraseMode property is no longer supported and will error in a future release.
Deepa Maheshvare
Deepa Maheshvare le 3 Août 2021
Modifié(e) : Deepa Maheshvare le 3 Août 2021
@Rik, Thank you , I'm sorry but I am really confused at this point.
I want to do the following: define the value of Number in `classname` properties from another function `main`.
function main(arg)
classname.get_Number(arg)
end
classdef classname
properties (Constant)
%use a struct to store the defaults to keep all your static
%properties in one place
Number=classname.get_Number;
end
methods(Static)
function val=get_Number(arg)
val=arg;
end
end
end
>>main(10)
Expected result:
When I call classname.Number from other functions or class, I want it to return 10.
Rik
Rik le 3 Août 2021
Modifié(e) : Rik 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
Deepa Maheshvare le 3 Août 2021
Thanks again.
My end goal is the following.
I have a bunch of classes and functions which are called from the main.m file and `classname` is one such class. I am using Application compiler to create a standalone application.
Problem:
In `classname` all the settings defined to run my code. This properties and methods in this class are accessed by the main script and by other functions that are called in the main script . While running main.m in MATLAB, I directly change the settings in `classname` depending on the test case that I want to run (e.g input values or turning on/off some calculations in the rest of the files by defining bool variables). The problem here is, I can pass input arguments only to main.m function while running the executable. So I am trying to figure out if there is a way to set the value of variables defined in `classname` by calling the methods defined in it via the main function.
Rik
Rik le 3 Août 2021
That sounds like you need a normal property, not a constant.
Thank you, I did
classdef classname
properties
PropName
propname2
end
methods(Static)
function obj = fun1(arg1,arg2)
obj.PropName = arg1;
obj.propname2 = arg2;
end
end
end
function main(arg)
classname.fun1(arg,arg)
end
After calling classname from main, I try accessing the properties in it .
a=classname
returns
a =
classname with properties:
PropName: []
propname2: []
I expected it to return
classname with properties:
PropName: 10
propname2: 10
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)
ans = struct with fields:
PropName: 10 propname2: 10
function main(arg)
classname.fun1(arg,arg)
end
Basically I want it to be a storage.
I think I could do the following. Defining persistent variable inside class helps in accessing the value globally without having to pass the input argument everytime.
classdef StoreData
methods (Static)
function out = setgetVar(data)
persistent Var;
if nargin
Var = data;
end
out = Var;
end
end
end
StoreData.setgetVar(10);
a = StoreData.setgetVar
ref:
https://in.mathworks.com/help/matlab/matlab_oop/static-data.html

Connectez-vous pour commenter.

Catégories

En savoir plus sur Argument Definitions dans Centre d'aide et File Exchange

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by