在MATLAB中从名称实例化类 [英] Instantiate class from name in MATLAB
问题描述
我试图列出我在Matlab文件夹中的某个文件夹中创建的类 - 仅使用他们的名字(类名)
I'm trying to list classes I created in some folder in my Matlab folder - using only their name (class name)
如果我知道它的名字是'SimpleString'
as an example, I have a class called 'SimpleString' - and I'm aiming to instantiate an object from that class, if all I know is that its name is 'SimpleString'
所以实时地,我想要从该类实例化一个对象。我想知道什么类是在一个文件夹(完成),然后能够实例化任何这些类(我的问题)
感谢
So in realtime, I'd like to find out what classes are in a folder (done), then be able to instantiate any of those classes (my question) Thanks
推荐答案
You can use eval
to instantiate the class using just the class name.
instance = eval('SimpleString');
但是,如果你只是遍历包含类定义的文件夹中的所有m文件,抓取文件名,您将只能使用此方法调用默认构造函数。
However, if you're simply iterating through all the m-files in a folder containing class definitions and grabbing the file names, you'll only be able to invoke the default constructor using this method.
这篇关于在MATLAB中从名称实例化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!