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.
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
stores a handle to the constructor
genericClassName
in the variablefuctionModel
. 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:
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: