Retrieve an object's name
Afficher commentaires plus anciens
I've used Matlab a long time (10+ years) but for the life of me I can't do something rather stupidly simple: get an object's name (not its class name but the name of a specific instance). For testing purposes, I just need a function to print out the name of a specific instance of an object. Surely there is a straightforward way to do this that I'm overlooking but it doesn't easily show up in any of the help retrievals I've tried. None of my fellow Matlab users here at work could give me any help. (I know I'm going to be red-faced when I learn (relearn?) the method.)
Réponse acceptée
Plus de réponses (2)
Christopher Ison
le 8 Juil 2011
4 commentaires
This is a very neat solution. I do it like this now:
methods
function name = objName(self)
name = inputname(1);
end
end
In this way you get your objects Name by
yourobject.objName
The Problem is, this works perfectly from outside the object, but it doesn't works in another method.
methods
function displayname(obj)
display(obj.objName)
end
end
Then the output is just
obj
So the new Task is: Retrieve an object's name WITHIN A METHOD
any ideas?
Sean de Wolski
le 24 Mar 2015
Lucas Schneeberger
le 1 Avr 2020
Modifié(e) : Lucas Schneeberger
le 1 Avr 2020
Is there a way to get the name of the instance inside the class constructor ? I would like to assign a value to obj.name property during the object construction.
This would require the left part of the assignment as in
instance_name = Class(varargin)
to be already processed when constructing the object, I am not sure if it is the case ...
Steven Lord
le 1 Avr 2020
I would like to assign a value to obj.name property during the object construction.
See the "Initializing Objects in Constructor" section on this documentation page for several examples of how to do this. There's no need to know with what name the class constructor was called to store the output (or even if that output has a name: mycell{1} = myObjectConstructor; is a perfectly valid way to call an object constructor and store the resulting object in a cell in a cell array.)
Paul O'Brien
le 13 Nov 2018
0 votes
In MatLab R2016b, if I have a variable "obj" referring to an object, you can get a list of methods by typing
obj.
and then pressing the tab key. One of the methods is called "string", and
obj.string
yields a string containing the name of the object.
1 commentaire
Steven Lord
le 13 Nov 2018
That assumes that the class author has chosen to overload the string method to behave that way. As an example of where this could fail is the table class:
>> t = array2table(magic(4));
>> class(t)
ans =
'table'
>> string(t)
Error using string
Conversion to string from table is not possible.
This is one case where string(t) and t.string behave differently, but neither gives the name of the variable.
>> t.string
Unrecognized variable name 'string'.
Catégories
En savoir plus sur Text Detection and Recognition dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!