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

5 vues (au cours des 30 derniers jours)
Brent Wyk
Brent Wyk le 22 Mai 2014
Modifié(e) : Cedric le 17 Juin 2020
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.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 22 Mai 2014
Modifié(e) : Sean de Wolski le 23 Mai 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 commentaires
Cedric
Cedric le 29 Nov 2017
Modifié(e) : Cedric le 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 le 20 Déc 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.

Connectez-vous pour commenter.

Plus de réponses (2)

Geoff Hayes
Geoff Hayes le 22 Mai 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 le 17 Juin 2020
How is it then possible to solve this same problem with MatlabR2019b?
  1 commentaire
Cedric
Cedric le 17 Juin 2020
Modifié(e) : Cedric le 17 Juin 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by