필터 지우기
필터 지우기

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

조회 수: 32 (최근 30일)
Apoorva Nagendra
Apoorva Nagendra 2018년 8월 29일
편집: Adam 2018년 8월 29일
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
Stephen23
Stephen23 2018년 8월 29일
편집: Stephen23 2018년 8월 29일
"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 2018년 8월 29일
편집: Adam 2018년 8월 29일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

J. Alex Lee
J. Alex Lee 2018년 8월 29일
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
J. Alex Lee
J. Alex Lee 2018년 8월 29일
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 2018년 8월 29일
I will try both the options and get back on which worked best. Thank you so much for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2018년 8월 29일
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.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by