MATLAB R2016a introduced a documented mechanism for restricting the class of the properties of an object (I'm aware that there's also an older, different, undocumented way to do that, but I'm not using that method). For example:
classdef MyClass
properties
myProperty RestrictedClass
end
end
will restrict the property myProperty
so that its values must be of class RestrictedClass
.
If you've implemented such a class, is there a way to use MATLAB's class metadata interface to programmatically discover the class of the restricted property?
I was hoping that if I used
m = ?MyClass;
p = m.PropertyList(1);
then the metaproperty object p
would contain information about the class to which it was restricted - but it seems not.
Is there a way to programmatically discover it? The method would need to not involve actually instantiating the class - both because that might have side effects, and also because it would need to work with Abstract classes.
PS Also not involving anything gross like parsing the source code of the class. Imagine that I might like to do this with a class whose source code had been obfuscated using pcode
.
When obj is an array, properties returns the properties of the class of the array. p = properties(___) returns the property names in a cell array of character vectors.
className = class( obj ) returns the name of the class of obj .
classdef Syntax Class definitions are blocks of code that are delineated by the classdef keyword at the beginning and the end keyword at the end. Files can contain only one class definition.
v = get(h) returns all properties and property values for the graphics object identified by h . v is a structure whose field names are the property names and whose values are the corresponding property values. h can be a single object or an m -by- n array of objects.
You can use the undocumented hack of converting an object to a structure in order to inspect the non-public properties of the object. The meta.Property
objects contain a hidden meta.Validation
object property, which has the validation information for the property:
Validation with properties:
Class: [1×1 meta.class]
Size: [1×0 meta.ArrayDimension]
ValidatorFunctions: {1×0 cell}
With a given example class:
classdef SOcode
properties
myProperty1 double
myProperty2 struct
end
end
We can inspect the meta.PropertyList
to locate the values we need:
>> test = struct(codemeta.PropertyList(1))
test =
struct with fields:
Name: 'myProperty1'
... snip ...
Type: [1×1 meta.type]
Validation: [1×1 meta.Validation]
DefiningClass: [1×1 meta.class]
... snip ...
We can then do something like the following:
codemeta = ?SOcode;
nprops = numel(codemeta.PropertyList);
validationclass = cell(nprops, 1);
for ii = 1:nprops
validationclass{ii} = codemeta.PropertyList(ii).Validation.Class.Name;
end
And receive:
>> validationclass
validationclass =
2×1 cell array
{'double'}
{'struct'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With