MATLAB: function to display the name of an instance of a class (object)

I'm stuck with what I expect should be something relatively simple. I am writing a class Superclass

such that:

 Superclass < handle

      

and then:

MyClass < Superclass

      

MyClass

contains a function DisplayObjectName

that should do what the name suggests. That is, display the name of the instance of the class (object).

For example, I create an object:

TestObject = MyClass(inputvariable);

      

Then I would like to have a function such that when calling

TestObject.DisplayObjectName()

      

the way out will be

ans = TestObject

      

I couldn't find a way to do this. Any ideas? Any help would be much appreciated.

+3


source to share


1 answer


The function inputname

seems to do the trick.

classdef SuperClass < handle
    methods
        function displayObjectName(self)
            disp(inputname(1))
        end
    end
end

      

Then



classdef MyClass < SuperClass
end 

      

and

>> TestObject = MyClass;
>> TestObject.displayObjectName
TestObject

      

+4


source







All Articles