How can I return a char of object variable name from a method
5 次查看(过去 30 天)
显示 更早的评论
How can I return a variable that represents a string of the variable name of an object, using a method of that object?
Here is a simple example:
classdef whatsMyName < handle
methods
function obj = whatsMyName()
end
function [ out ] = myNameIs(obj)
out = inputname(1);
fprintf('(1)my name is %s\n', out)
end
function [ out ] = orMaybeMyNameIs(obj)
out = obj.myNameIs;
fprintf('(2)my name is %s\n', out)
end
end
end
>> w.myNameIs;
(1)my name is w
>> w.orMaybeMyNameIs;
(1)my name is obj
(2)my name is obj
I'd like to be able to return "w" from the call inside a method.
0 个评论
采纳的回答
Sean de Wolski
2014-5-22
编辑:Sean de Wolski
2014-5-23
Why not just store it as a property?
More
If it ONLY needs to work from one method calling _oneother methods (i.e. not method1 calling method2 asking for name), you can use evalin:
name = evalin('caller','inputname(1)')
Class:
classdef whatsMyName < handle
methods
function obj = whatsMyName()
end
function [ out ] = myNameIs(obj)
out = evalin('caller','inputname(1)')
fprintf('(1)my name is %s\n', out)
end
function [ out ] = orMaybeMyNameIs(obj)
out = obj.myNameIs;
fprintf('(2)my name is %s\n', out)
end
end
end
Example:
howdy = whatsMyName
howdy =
whatsMyName with no properties.
orMaybeMyNameIs(howdy)
out =
howdy
(1)my name is howdy
(2)my name is howdy
ans =
howdy
7 个评论
Cedric
2017-11-29
编辑:Cedric
2017-11-29
While this was not part of the question, I should add that this works because
howdy.myNameIs()
is equivalent to
myNameIs(howdy)
and howdy is hence the variable name of the first input. This will fail if howdy is e.g. a field or a property name:
S.howdy = whatsMyName ;
S.howdy.myNameIs() ;
yields
(1)my name is
ans =
0×0 empty char array
because in myNameId(S.howdy), input 1 S.howdy is not a variable name (for INPUTNAME).
This implies that an object saved as a property of another object cannot know its "property name" (at least through this means).
PS: Sean, if you knew a way for an object to know its name as a property of another object, I could use it.
Philip Borghesani
2017-12-20
One more addendum to this answer:
out = evalin('caller','inputname(1)')
will not work in MATLAB R2015b or later versions of MATLAB due to a change in inputname. Inputname no longer respects the context of evalin statements or code directly called by one.
更多回答(2 个)
Geoff Hayes
2014-5-22
The following method (once added to your above class) seems to do what I think you want:
function [nm] = getMyName(~)
nm = inputname(1);
end
Just clear all and try again:
>> obj1 = whatsMyName;
>> obj1.getMyName
obj1
>> longerName = whatsMyName;
>> longerName.getMyName
longerName
0 个评论
Anna Marcellan
2020-6-17
How is it then possible to solve this same problem with MatlabR2019b?
1 个评论
Cedric
2020-6-17
编辑:Cedric
2020-6-17
You should redesign your approach so you don't have to rely on the object variable name.
Assuming that you can access that name, what happens if a user of your class does the following, for example:
myObjects{5} = MyClass() ;
If the name contains relevant information, this information could be passed to the constructor as a string/char array and saved in a property:
temp = MyClass('temperature');
speed = MyClass('speed');
If you absolutely need to get the variable name, you should create a new thread and refer to this one.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!