Azzera filtri
Azzera filtri

How can I get the name of the class object as a string?

33 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 29 Ago 2018
Modificato: Stephen23 il 29 Ago 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
Adam il 29 Ago 2018
Modificato: Adam il 29 Ago 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.

Accedi per commentare.

Risposta accettata

J. Alex Lee
J. Alex Lee il 29 Ago 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 Commenti
J. Alex Lee
J. Alex Lee il 29 Ago 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".
Apoorva Nagendra
Apoorva Nagendra il 29 Ago 2018
I will try both the options and get back on which worked best. Thank you so much for your help!

Accedi per commentare.

Più risposte (1)

Jan
Jan il 29 Ago 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.

Community Treasure Hunt

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

Start Hunting!

Translated by