How can I return a char of object variable name from a method

6 views (last 30 days)
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.

Accepted Answer

Sean de Wolski
Sean de Wolski on 22 May 2014
Edited: Sean de Wolski on 23 May 2014
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 Comments
Cedric
Cedric on 29 Nov 2017
Edited: Cedric on 29 Nov 2017
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
Philip Borghesani on 20 Dec 2017
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.

Sign in to comment.

More Answers (2)

Geoff Hayes
Geoff Hayes on 22 May 2014
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

Anna Marcellan
Anna Marcellan on 17 Jun 2020
How is it then possible to solve this same problem with MatlabR2019b?
  1 Comment
Cedric
Cedric on 17 Jun 2020
Edited: Cedric on 17 Jun 2020
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!