MATLAB: Using Property Blocks : Set/get on properties within a single property block

classdefmethodsooppropertiesproperty blocks

Hello all,
Yet another classdef question from me. Let us say I have a bunch of properties for some custom class, and that some are data properties that are numerical arrays, and some are metadata properties that are of mixed classes. It is useful to use the following:
classdef myClass
properties % Metadata
name char
author char
date datetime
end
properties % Data
a (:,1) double
b (:,1) double
c (:,1) double
d (:,1) double
end
end
Now, I may want to index into my data properties or, more likely, truncate a single object or concatenate two of them. Now, I clearly want to only trunc/cat the data properties, but for now I am stuck adding a method like the following:
function obj = trunc(obj,s,e)
obj.a = obj.a(s:e);
obj.b = obj.b(s:e);
obj.c = obj.c(s:e);
obj.d = obj.d(s:e);
end
function obj = cat(obj,obj2)
obj.a = cat(1,obj.a,obj2.a)
obj.b = cat(1,obj.b,obj2.b)
obj.c = cat(1,obj.a,obj2.c)
obj.d = cat(1,obj.b,obj2.d)
end
Nominally I would have something like this pseudocode
function obj = trunc(obj,s,e)
datProps = properties(obj,'block','Data')
for ii = 1:length(datProps)
obj.(datProps{ii}) = obj.(datProps{ii})(s:e)
end
end
and similarly for cat. Is there any way of doing this? It would be extremely useful for classes with a large number of properties.
Side note; I have a parallel concern with the results of doc myClass as it currently returns all of the properties but in an alphabetical/case sorted list. I can think of a number of ways to leverage both properties and methods blocks if both the comments, and a block label, could be attached, and/or if functions could apply only to properties within a set block.
After writing, this may be better for suggestions, but maybe someone has an idea for how to do this in 2019?
-DP

Best Answer

The problem is obviously to implement 'block','Data'.
Try this
>> myc = myClass
myc =
myClass with properties:
name: ''
author: ''
date: [0×0 datetime]
a: [100×1 double]
b: [100×1 double]
c: [100×1 double]
d: [100×1 double]
>> myc = myc.trunc(10,90)
myc =
myClass with properties:
name: ''
author: ''
date: [0×0 datetime]
a: [81×1 double]
b: [81×1 double]
c: [81×1 double]
d: [81×1 double]
>>
where
classdef myClass < myData
properties % Metadata
name char
author char
date datetime
end
methods
function this = trunc( this, ixb, ixe )
mc = metaclass( this );
len = length( mc.PropertyList );
for jj = 1 : len
if strcmp( mc.PropertyList(jj).DefiningClass.Name, 'myData' )
this.( mc.PropertyList(jj).Name ) ...
= this.( mc.PropertyList(jj).Name )(ixb:ixe);
end
end
end
end
end
and
classdef myData
properties % Data
a (:,1) double = randn( 100,1);
b (:,1) double = randn( 100,1);
c (:,1) double = randn( 100,1);
d (:,1) double = randn( 100,1);
end
end
I don't know, but if nothing else this is an exercise with meta.class.
Maybe, a better alternative would be to put a,b,c,d as variables in a table.