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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

per isakson
per isakson am 12 Jun. 2019
Bearbeitet: per isakson am 12 Jun. 2019
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.
  1 Kommentar
D. Plotnick
D. Plotnick am 13 Jun. 2019
Thanks, there are a lot of interesting pieces in there that I hadn't seen before (I have been getting properties using propList = properties(this)) and had totally forgottten about metaclass.
I have opted for a variation on your final suggestion, which is in my comment to Steven Lord below.
For now, it sounds like this is not a native capability of Matlab and has to be shoehorned in. I think I will submit it as a suggestion.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 12 Jun. 2019
I second some of Jan's suggestions about perhaps packing the properties into a table array (or maybe a struct, vector, or even an object.) I wanted to comment on a different part of your question, your side note about object display. This might even help with your truncation question now that I think about it.
You can use the matlab.mixin.CustomDisplay base class to customize how your class gets displayed. See the documentation for this capability for some examples. You could write a getPropertyGroups method to divide the properties of your object into related collections of properties that are displayed as separate groups.
You could even potentially abuse that system a little bit to help with your truncation question. MATLAB will only call getPropertyGroups with one input to customize the display. You could have your version of that method additionally allow a two input syntax where the inputs are the object and a 'type' of property ('Data' or 'Metadata' in the example class you showed.) The two input syntax would return a list of properties of the type you requested. You could then use dynamic property referencing to refer to those properties like the below example, which accesses the table property named Properties using the char vector 'Properties' to create P2.
T = array2table(magic(4));
P1 = T.Properties;
P2 = T.('Properties');
isequal(P1, P2)
  3 Kommentare
Steven Lord
Steven Lord am 13 Jun. 2019
If you feel both answers were useful, you could accept one and vote for the other (or accept one and vote for both.)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Class File Organization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by