Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Get class name in superclass static method

Subject: Get class name in superclass static method

From: Kenneth

Date: 20 Mar, 2009 18:27:01

Message: 1 of 7

Suppose I have two classes:

classdef MySuper < handle
  methods (Static)
     function name = contrived_example()
        % This does not work right
        name = mfilename('class');
     end
   end
end

classdef MyClass < MySuper; end

Then,
MyClass.contrived_example() --> 'MySuper'

There's certainly a good reason for that behavior, but I'd really like a function that would return 'MyClass' in that example, and I can't seem to get at the metaclass without first having an instance or knowing the name. Any ideas? I suppose for my purposes I can pass the name or metaclass itself in as an argument, but this is not ideal for all situations.

Subject: Get class name in superclass static method

From: Rich Ellis

Date: 24 Mar, 2009 16:30:28

Message: 2 of 7

Would the superclasses function help you here? Perhaps pass "name" to this
function.
>>help superclasses
 SUPERCLASSES CLASSNAME displays the names of all visible superclasses
    of the MATLAB class with the name CLASSNAME. Visible classes are those
    with class attribute Hidden set to false (the default).

    SUPERCLASSES(OBJECT) displays the names of the visible superclasses for
    the class of OBJECT, where OBJECT is an instance of a MATLAB class.
    OBJECT can be either a scalar object or an array of objects.

    S = SUPERCLASSES(...) returns the superclass names in a cell array of
    strings.


"Kenneth " <kwatford@gmail.com> wrote in message
news:gq0n5l$gi5$1@fred.mathworks.com...
> Suppose I have two classes:
>
> classdef MySuper < handle
> methods (Static)
> function name = contrived_example()
> % This does not work right
> name = mfilename('class');
> end
> end
> end
>
> classdef MyClass < MySuper; end
>
> Then,
> MyClass.contrived_example() --> 'MySuper'
>
> There's certainly a good reason for that behavior, but I'd really like a
> function that would return 'MyClass' in that example, and I can't seem to
> get at the metaclass without first having an instance or knowing the name.
> Any ideas? I suppose for my purposes I can pass the name or metaclass
> itself in as an argument, but this is not ideal for all situations.

Subject: Get class name in superclass static method

From: Kenneth

Date: 24 Mar, 2009 17:10:17

Message: 3 of 7

No, I'm not looking for the superclass name. I want the current (subclass's) name.

"Rich Ellis" <rich@mathworks.com> wrote in message <gqb1r6$4rs$1@fred.mathworks.com>...
> Would the superclasses function help you here? Perhaps pass "name" to this
> function.

Subject: Get class name in superclass static method

From: Darik

Date: 21 May, 2009 17:37:01

Message: 4 of 7

"Kenneth " <kwatford@gmail.com> wrote in message <gq0n5l$gi5$1@fred.mathworks.com>...
> Suppose I have two classes:
>
> classdef MySuper < handle
> methods (Static)
> function name = contrived_example()
> % This does not work right
> name = mfilename('class');
> end
> end
> end
>
> classdef MyClass < MySuper; end
>
> Then,
> MyClass.contrived_example() --> 'MySuper'
>
> There's certainly a good reason for that behavior, but I'd really like a function that would return 'MyClass' in that example, and I can't seem to get at the metaclass without first having an instance or knowing the name. Any ideas? I suppose for my purposes I can pass the name or metaclass itself in as an argument, but this is not ideal for all situations.


Hey Kenneth,

Did you (or anybody else) ever find a solution or workaround to this?

Subject: Get class name in superclass static method

From: Kenneth

Date: 21 May, 2009 18:03:01

Message: 5 of 7

> Hey Kenneth,
>
> Did you (or anybody else) ever find a solution or workaround to this?

Nope, reflection in MATLAB is still wonky.

Subject: Get class name in superclass static method

From: Jonas

Date: 16 Jun, 2009 14:06:02

Message: 6 of 7

"Kenneth " <kwatford@gmail.com> wrote in message <gv450k$l1f$1@fred.mathworks.com>...
> > Hey Kenneth,
> >
> > Did you (or anybody else) ever find a solution or workaround to this?
>
> Nope, reflection in MATLAB is still wonky.

A workaround to find the class name if the static method was invoked from the command line could be to parse the last line in the command history.

Does anyone know how to access the command history programmatically?

Subject: Get class name in superclass static method

From: Jonas

Date: 16 Jun, 2009 14:20:17

Message: 7 of 7

Never mind, I figured it out. It is a hack, but it seems to work quite fine as long as you invoke the static method from command line. If you do it programmatically, you should in general know which class you are invoking, and you can pass the name to the static method.

The static method should look something like this (the regular expression needs a bit more work so that it doesn't fail if there is no space before the equal sign)

methods (Static)
        function name = contrieved_example
        % get the command history
 javaHistory=com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
        lastCommand = javaHistory(end).toCharArray';
        % find string before 'contrieved_example'
        tmp = regexp(lastCommand,'(\w+)\.contrieved_example','tokens');
        name = tmp{1}{1};
        end
    end
end

"Kenneth " <kwatford@gmail.com> wrote in message <gq0n5l$gi5$1@fred.mathworks.com>...
> Suppose I have two classes:
>
> classdef MySuper < handle
> methods (Static)
> function name = contrived_example()
> % This does not work right
> name = mfilename('class');
> end
> end
> end
>
> classdef MyClass < MySuper; end
>
> Then,
> MyClass.contrived_example() --> 'MySuper'
>

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us