3

I am stuck with what I expect should be something relatively simple to do. I am writing a class Superclass such that:

 Superclass < handle

and then:

MyClass < Superclass

MyClass contains function DisplayObjectName, which should do what the name suggests. That is, display the name of the class instance (object).

For example, I create an object:

TestObject = MyClass(inputvariable);

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

TestObject.DisplayObjectName()

the output would be

ans = TestObject

I could not seem to find a way to do that. Any ideas? Any help would be very much appreciated.

4

1 回答 1

4

inputname功能似乎可以解决问题。

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

然后

classdef MyClass < SuperClass
end 

>> TestObject = MyClass;
>> TestObject.displayObjectName
TestObject
于 2013-01-21T22:45:35.580 回答