假设我在类文件中定义了一个 MATLAB 对象
classdef foo
properties
bar
end
end
然后我创建了一个 foo 对象
myfoo = foo();
现在我想动态地向 foo 添加另一个字段。我想要的是
myfoo.newfield = 42;
但这会引发错误。
我知道有一种方法可以动态地将字段/属性添加到 MATLAB 对象,但我不记得了,也无法在帮助中轻松找到它。有人知道语法吗?
最佳答案
好的,找到了。但它并不通用,只有 dynamicprops 类的子类实现了它。 这就是我记得遇到的事情。所以我怀疑这个问题的一般答案是你做不到。
Any class that is a subclass of the dynamicprops class (which is itself a subclass of the handle class) can define dynamic properties using the addprop method. The syntax is:
P = addprop(H,'PropertyName')
关于matlab - 在matlab中为对象动态添加一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2998675/