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

조회 수: 8(최근 30일)
D. Plotnick 3 May 2019
댓글: D. Plotnick 14 Jun 2019
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
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

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

### 채택된 답변

per isakson 12 Jun 2019
편집: per isakson 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
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표시숨기기 없음
D. Plotnick 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.

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

### 추가 답변(1개)

Steven Lord 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표시숨기기 이전 댓글 수: 2
D. Plotnick 14 Jun 2019
Done! Also, submitted to feature requests.

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

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.85891026,"math_prob":0.67277867,"size":1728,"snap":"2021-21-2021-25","text_gpt3_token_len":450,"char_repetition_ratio":0.12935035,"word_repetition_ratio":0.0,"special_character_ratio":0.25983796,"punctuation_ratio":0.19323671,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95366037,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-09T21:59:35Z\",\"WARC-Record-ID\":\"<urn:uuid:ff21061b-297b-42c8-b2d2-f4a216cb7b41>\",\"Content-Length\":\"169158\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:75ed0295-e22e-4390-93cc-1829fe3cdc40>\",\"WARC-Concurrent-To\":\"<urn:uuid:45256e37-dcbe-4e83-8d80-4f863fcae63f>\",\"WARC-IP-Address\":\"23.210.0.55\",\"WARC-Target-URI\":\"https://kr.mathworks.com/matlabcentral/answers/460125-using-property-blocks-set-get-on-properties-within-a-single-property-block?s_tid=prof_contriblnk\",\"WARC-Payload-Digest\":\"sha1:VLTOFIYR25LCPLA3TGRR4Y3S26PFZ656\",\"WARC-Block-Digest\":\"sha1:IZL5L3UGJW3C4YRWMVP3ICDLVYYU4AGK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243989018.90_warc_CC-MAIN-20210509213453-20210510003453-00220.warc.gz\"}"}