Filter löschen
Filter löschen

dynamic property subclass use correct get and set methods

2 Ansichten (letzte 30 Tage)
Rajmohan
Rajmohan am 18 Mai 2022
Beantwortet: Ishan Gupta am 27 Jul. 2022
The superclass definition is -
classdef calibration ... % Class name
< dynamicprops %% Superclass
% dynamicprops makes handle class
% Creation Properties
properties (SetAccess = immutable)
created = datestr(now, 'yyyymmddTHHMMSS');
author = getenv('username');
end
% Properties
properties
name = '';
end
% Dynamic Property
properties (SetAccess = protected, Hidden = true)
metaDynProp = [];
end
methods
val = get(obj)
set(obj)
end
% Methods
methods
function addCal(obj,calName,calVal)
% ADDCAL adds calibration to a cal object
% Determine calibration type
if isscalar(calVal)
caseNum = 1;
elseif isstruct(calVal) && length(calVal) == 4
caseNum = 2;
elseif isstruct(calVal) && length(calVal) == 6
caseNum = 3;
else
error('Invalid Calibration')
end
% Create Calibration
obj.metaDynProp.(calName) = obj.addprop(calName);
%obj.metaDynProp.(calName).SetAccess = 'private';
switch(caseNum)
case 1 % Scalar case
% Assign Methods
obj.(calName) = scalar;
%obj.metaDynProp.(calName).SetMethod = 'set@scalar';
%obj.metaDynProp.(calName).GetMethod = @scalar.get;
case 2 % Table case
% Assign Methods
case 3 % Map case
% Assign Methods
end
% Set Property
set(obj.(calName),calVal);
end
end
end
The subclass definition is
classdef scalar < calibration
properties
value = [];
end
% Methods
methods
function set(obj,value)
obj.value = value;
end
function value = get(obj)
value = obj.value;
end
end
end
I would like to have the functionality where a calibration can be added such as
x = calibration;
x.addCal('a',1);
Then when x.a is called, the output is 1. However, with the above implementation the output is a scalar object. Also, is there a way to prevent properties from being inherited?
  1 Kommentar
Seth Furman
Seth Furman am 24 Mai 2022
Note that, for datetime formats
  • M corresponds to month
  • m corresponds to minute
  • s corresponds to second
  • S corresponds to fractional second
so
created = datestr(now, "yyyymmddTHHMMSS");
as a datetime would be
created = datetime("now", Format="yyyyMMdd'T'HHmmss")
created = datetime
20220524T174204

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ishan Gupta
Ishan Gupta am 27 Jul. 2022
You can set property as Private to avoid getting inherited.
Please refer this.

Kategorien

Mehr zu Class Introspection and Metadata finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by