Custom class properties and method attributes

30 views (last 30 days)
Is there a way to add custom property or method attributes to handle classes? It is done by several classes or toolboxes, e.g. the unit testing framework allows to add tags to the methods: Tag Unit Tests - MATLAB & Simulink (mathworks.com)
classdef ExampleTagTest < matlab.unittest.TestCase
methods (Test)
function testA (testCase)
% test code
end
end
methods (Test, TestTags = {'Unit'})
function testB (testCase)
% test code
end
function testC (testCase)
% test code
end
end
I'm talking about the attributes "Test" and "TestTags".
My application would greatly benetfit from a finer granular control of properties for a DTO (data transfer object) class. I want to use argument validation but add additional options like overridable properties and don't want to use inputParser anymore :)
Thanks!
  4 Comments
Jan Kappen
Jan Kappen on 26 Oct 2021
You're right - a custom validation function will be used. But I'd nevertheless like to use the property validation combined with a separate validation function.
Reasons are simple, arguments get casted to expected data types and early errors are thrown when mandatory (non-default) properties are missing (to be set by the constructor). I can see, what properties (a.k.a input parameters) are available and I can use meta class/property data to iterate through all of them etc.
Plus, I have awesome auto completion proposals, via
function obj = SensorYieldAnalysisHelper(PropertyArgs)
%SensorYieldAnalysisHelper Construct an instance of this class
% Detailed explanation goes here
arguments
PropertyArgs.?SensorYieldAnalysisHelper % support all public properties AND automatic parameter completion!
end
end
I mean, how cool is that?
Does anything above work with inputParser (honest question!)?

Sign in to comment.

Accepted Answer

Matt J
Matt J on 26 Oct 2021
I don't know why it's not documented, but apparently there is a Description attribute that can be added, e.g.,
classdef myclass
properties (Description='propTag')
prop
end
methods (Description='methTag')
function meth1
end
end
methods
function meth2
end
end
end
  2 Comments
Matt J
Matt J on 26 Oct 2021
Edited: Matt J on 26 Oct 2021
Would you expect that Description field to be existent in the next releases?
Since it's not documented, it's hard to know for certain. Hopefully, someone from the MathWorks will weigh in.
Does this address your question, though? If so, I'd be much obliged if you would Accept-click the answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Class Introspection and Metadata in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!