defining properties in a class

조회 수: 5 (최근 30일)
Deepa Maheshvare
Deepa Maheshvare 2021년 8월 3일
댓글: Deepa Maheshvare 2021년 8월 3일
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`?

답변 (1개)

Rik
Rik 2021년 8월 3일
편집: Rik 2021년 8월 3일
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
Rik
Rik 2021년 8월 3일
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
Deepa Maheshvare
Deepa Maheshvare 2021년 8월 3일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Class File Organization에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by