MATLAB: How to return a char of object variable name from a method

object-orientedoop

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.

Best Answer

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