gpt4 book ai didi

matlab - 在matlab中为对象动态添加一个字段

转载 作者:太空宇宙 更新时间:2023-11-03 19:43:10 25 4
gpt4 key购买 nike

假设我在类文件中定义了一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com