Reputation: 5960
I have some classes that I created a couple of constant properties for. Here is some example code:
classdef genericClassName < handle
properties (Constant)
Name = 'genericClassName'
Description = 'description of the class'
end
...
In the main code I create objects by assigning the class handle, which in this case comes from a pre-assigned value delivered from a separate function. It would be like
fuctionModel = @genericClassName;
and later I'll create other objects and pass the value of functionModel to those classes. Up to this point, it all works fine.
The Matlab documentation says that these constant properties are accessed like this:
genericClassName.Name
genericClassName.Description
I can type that into the command line and it produces the desired result, giving the value of the Name or Description property - the same values assigned to the constant properties. However, I only have the handle, which is saved in functionModel as @genericClassName.
This is my question: How can I refer to this class and its constant properties when I only have the handle, with its at-sign prepended?
Update Short of a simpler or concise answer, a combination of the answers from @Edric and @CrisLuengo seems to work. For instance:
mc=meta.class.fromName(func2str(functionalModel));
result = eval([mc.Name '.Description']);
puts the constant with the name Description into the variable result. This is usable for what I need, and I'll probably just wrap it into a function.
Upvotes: 0
Views: 927
Reputation: 5960
Short of a simpler or concise answer, a combination of the answers from @Edric and @CrisLuengo seems to work. For instance:
mc=meta.class.fromName(func2str(functionalModel));
result = eval([mc.Name '.Description']);
puts the constant with the name Description into the variable result. This works well enough when wrapped in a function.
Upvotes: 0
Reputation: 60434
There is no such thing as a class handle. I don’t think there is a way in MATLAB to reference a class other than through its name (either directly in code or as a string). Your code
fuctionModel = @genericClassName;
stores a handle to the constructor genericClassName
in the variable fuctionModel
. With this handle, you can construct objects of your class, but you cannot do anything else. This is not a reference to the class itself.
It is unclear why you have set up your code this way. However, the easiest way to access your constants through this handle is to first create an object of your class:
a = fuctionModel();
a.Name
Edric suggests an alternative, also not convenient or pretty.
A third alternative, which I’m not advocating for (this can cause slowdown in your code, and is hard to read and thus maintain) is to store the name of your class as a string, then use eval
to get your value:
fuctionModel = 'genericClassName';
eval([fuctionModel,'.Name']);
Upvotes: 2
Reputation: 25140
Hm, if you have only a handle to the constructor method, and you wish to avoid constructing an instance (MATLAB allows you to access Constant
properties from instances), then here's a way you can do it using meta.class.fromName
.
fh = @genericClassName;
% Get the metaclass from the constructor
mc = meta.class.fromName(func2str(fh));
% Find the property named 'Name'
idx = strmatch('Name', {mc.PropertyList.Name})
% Get the default (Constant) value
mc.PropertyList(idx).DefaultValue
Upvotes: 3