필터 지우기
필터 지우기

Get all allowed values of a class property

조회 수: 3 (최근 30일)
Alexander Jöhl
Alexander Jöhl 2021년 3월 29일
댓글: Matt J 2021년 3월 29일
I have a class that has a property restricted to certain values (mustbemember several char arrays in this case). I also have an app that contains an object of this class. The app also has a dropdown. Now, I want to populate the dropdown item list with the allowed values of that restricted property. Is it possible to programmatically get a list of all the allowed values of the class?
  댓글 수: 2
J. Alex Lee
J. Alex Lee 2021년 3월 29일
i don't know if this is a good way, but since it sounds like you are the one authoring the class and controlling by comparison to lists, can you just expose that list?
Alexander Jöhl
Alexander Jöhl 2021년 3월 29일
Thanks for your comment:
The property looks like this:
prop char {mustBeMember(prop,{'a','b','c'})} = 'a'
The list is hard coded here. I don't know how to extract it from there.

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

채택된 답변

Matt J
Matt J 2021년 3월 29일
편집: Matt J 2021년 3월 29일
Below is a rather gymnastic way of extracting the list (I admit it would be nice if this were unnecessary):
classdef myclass %Example class
properties
prop {mustBeMember(prop,{'a','b','c'})} = 'a'
end
end
m=?myclass;
str = func2str( m.PropertyList.Validation.ValidatorFunctions{1} );
C=extractBetween(str,'{','}','Boundaries','inclusive');
selections=eval(C{1})
selections = 1×3 cell array
{'a'} {'b'} {'c'}
  댓글 수: 1
Alexander Jöhl
Alexander Jöhl 2021년 3월 29일
Thanks for your answer. A minor point I noticed: if you have multiple properties, then you have to index into the PropertyList:
str = func2str( m.PropertyList(1).Validation.ValidatorFunctions{1} );

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

추가 답변 (1개)

J. Alex Lee
J. Alex Lee 2021년 3월 29일
편집: J. Alex Lee 2021년 3월 29일
Ah, I should more fully embrace the validator mechanism. Here is a way by defining a custom validator, so in a way less hard-coded by relying on the same mechanism.
classdef myclass %Example class
properties (Constant)
AllowedPropVals = {'a','b','c'}
end
properties
prop {myclass.propValidator(prop)} = 'a'
propstat {mustBeMember(propstat,{'a','b','c'})} = 'a'
end
methods (Static , Access = private)
function propValidator(prop)
mustBeMember(prop,myclass.AllowedPropVals)
end
end
end
Then your app can simply access the constant property myclass.AllowedPropVals
  댓글 수: 3
J. Alex Lee
J. Alex Lee 2021년 3월 29일
I didn't either, until I saw your question and wondered about it, so thank you!
Matt J
Matt J 2021년 3월 29일
Funny that you can't just do,
properties
propstat {mustBeMember(propstat,myclass.AllowedPropVals)} = 'a'
end

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

카테고리

Help CenterFile Exchange에서 Methods에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by