How can I get the name of the class object as a string?
26 views (last 30 days)
Show older comments
I am writing a method in a class which returns the name of a class object which was previously created using certain settings. So I need to know how do I extract only the name of the object created for those settings. Any help is appreciated!
6 Comments
Stephen23
on 29 Aug 2018
Edited: Stephen23
on 29 Aug 2018
"I asked it because I need it. I cannot tell you why I need it, but I do."
So far every other time that someone has posted here saying "I need to magically access variable names" it has turned out to be incorrect and inefficient (in the sense that other ways of doing their task are simpler, faster, and less buggy):
So far you have not provided us with any reason to believe that this will be any different: I am genuinely interested to know this use-case is, that you require this kind of meta-programming. Please give some details!
Adam
on 29 Aug 2018
Edited: Adam
on 29 Aug 2018
From an object-orientation perspective, until I see a clear case otherwise I would stand by that 'A class should never be containing methods that try to work out what name was given to the objects of it that were created'.
Obviously if I see or hear of a counter-example I would reconsider, but it seems so fundamentally against program design that 'never' seems fair enough given it is only my opinion I am stating anyway. I can easily believe that design mistakes (of which I have made uncountably many) can lead to situations where this may seem like something that is needed, but at that point it is time to take a step back and re-examine the design decisions that got you to that point in the first place rather than just plough on through to whatever end.
Accepted Answer
J. Alex Lee
on 29 Aug 2018
One way I can think of is to use "evalin('base',...)" for your method to probe all the base workspace variables to check their properties (which is what I assume you mean by "settings"):
varlist = evalin('base','whos')
for i = 1:length(varlist)
if isa(varlist(i).class,myClassName)
% code to check property values or settings
end
end
2 Comments
J. Alex Lee
on 29 Aug 2018
Just also occurred to me that the result of whos could be passed as an argument to the method to eliminate need for "evalin".
More Answers (1)
Jan
on 29 Aug 2018
This sounds like meta-programming. If the actual program requires information about the implementation of the code, the complexity of the code is prone to exploding. Therefore meta-programming is often considered as a programming-anti-pattern, because if produces more problems than it solves.
Peeking with evalin in the caller's or base workspace fails, if the part of the code is moved to a subfunction. It is much cleaner not to include dependencies to the location of the code.
What about implementing a list a objects together with the required values? a very simple form of this method is using structs, where the fieldnames are stored easily also without any meta-programming. I still assume, that the need for accessing the names of objects is a confusion of the levels of data and code. It might be a massive simplification, to store names and data in lists instead of creating a connection to the details of the code.
0 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!