Is there a way to find all children of a Matlab class?

后端 未结 3 1632
孤城傲影
孤城傲影 2021-02-19 23:04

The Matlab function superclasses returns the names of all parents of a given class.

Is there an equivalent to find all classes derived from a given class, i

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 23:32

    The code

    I moved the code since it was > 200 lines onto the Github repository getSubclasses. You are welcome to reqeut features and post bug reports.

    Idea

    Given a root class name or a meta.class and a folder path, it will traverse down the folder structure and build a graph with all the subclasses derived from the root (at infinite depth). If the path is not supplied, then it will recurse down from the folder where the root class is located.

    Note, that the solution is local, and that is why it is fast, and relies on the assumption that subclasses are nested under some subfolder of the chosen path.

    Example

    List all subclasses of the sde class. You will need R2015b to be able to produce the graph, or you can use the output and the FEX submission plot_graph() to produce a dependency graph.

    getSubclasses('sde','C:\Program Files\MATLAB\R2016a\toolbox\finance\finsupport')

    And the output with the edges and node names:

     names      from    to
    ________    ____    __
    'sde'        1      1 
    'bm'         2      3 
    'sdeld'      3      6 
    'cev'        4      3 
    'gbm'        5      4 
    'sdeddo'     6      1 
    'heston'     7      6 
    'cir'        8      9 
    'sdemrd'     9      6 
    'hwv'       10      9 
    

    We can compare the result with the official documentation, which in this case lists the SDE hierarchy, i.e.

    Timing

    On Win7 64b R2016a

    • less than 0.1 seconds: getSubclasses('sde','C:\Program Files\MATLAB\R2016a\toolbox\finance\finsupport')
    • about 13 seconds if scanning the whole matlabroot: getSubclasses('sde',matlabroot);

提交回复
热议问题