Instantiate class from name in MATLAB

后端 未结 4 2054
一整个雨季
一整个雨季 2020-12-31 07:31

I\'m trying to list classes I created in some folder in my Matlab folder - using only their name (class name)

as an example, I have a class called \'SimpleString\' -

4条回答
  •  情话喂你
    2020-12-31 08:08

    Use a package to access class constructors with .()-notation.

    A Matlab package is simply a folder/directory with a name that begins with +:

    +mypackage/foo.m:

    classdef foo
        methods
            function obj = foo(arg1, arg2)
                %foo constructor
            end
        end
    end
    

    With class foo defined this way, you can access the constructor of mypackage.foo as

    class_name = 'foo';
    o = mypackage.(class_name)('arg1_value', 'arg2_value');
    

提交回复
热议问题