How can I return a char of object variable name from a method
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Brent Wyk
el 22 de Mayo de 2014
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 comentarios
Respuesta aceptada
Sean de Wolski
el 22 de Mayo de 2014
Editada: Sean de Wolski
el 23 de Mayo de 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 comentarios
Cedric
el 29 de Nov. de 2017
Editada: Cedric
el 29 de Nov. de 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
el 20 de Dic. de 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.
Más respuestas (2)
Geoff Hayes
el 22 de Mayo de 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
0 comentarios
Anna Marcellan
el 17 de Jun. de 2020
How is it then possible to solve this same problem with MatlabR2019b?
1 comentario
Cedric
el 17 de Jun. de 2020
Editada: Cedric
el 17 de Jun. de 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.
Ver también
Categorías
Más información sobre Argument Definitions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!