MATLAB - a list of all methods provided only by a subclass?

I have a class that inherits from several superclasses, and I would like to get the methods that the class has. Naive use methods()returns methods from the class I'm working with, as well as superclass methods, but I'm not interested in superclass methods.

Any ideas how to do this? I could not find anything in the MATLAB documentation.

Thank!

+5
source share
2 answers

If your subclass does not override any of the superclass methods (or if you do not notice overridden methods), you can use the METHODS and SUPERCLASSES functions to find a list of subclass methods that are also not methods of any of the superclasses. For instance:

>> obj = 'hgsetget';  %# A sample class name
>> supClasses = superclasses(obj)

supClasses = 

    'handle'    %# Just one superclass, but what follows should handle more

>> supMethods = cellfun(@methods,supClasses,...  %# Find methods of superclasses
                        'UniformOutput',false);
>> supMethods = unique(vertcat(supMethods{:}));  %# Get a unique list of
                                                 %#   superclass methods
>> subMethods = setdiff(methods(obj),supMethods)  %# Find methods unique to the
                                                  %#   subclass
subMethods = 

    'get'
    'getdisp'
    'set'
    'setdisp'
+3
source

Even if this question is resolved, let me add another answer using the possibilities meta.class:

%# some class name
clname = 'hgsetget';

%# obtain class meta-info
mt = meta.class.fromName(clname);

%# get name of class defining each method
cdef = arrayfun(@(c)c.Name, [mt.MethodList.DefiningClass], 'Uniform',false);

%# keep only methods that are defined in the subclass
subMethods = {mt.MethodList(ismember(cdef,clname)).Name}

The result for this example:

subMethods = 
    'set'    'get'    'setdisp'    'getdisp'    'empty'

Notice how the result also includes static methods emptythat all non-abstract classes have (used to create an empty array of this class).

+2
source

All Articles