是否可以即时将方法添加到MATLAB类? [英] Is it possible to add methods on the fly to MATLAB classes?
问题描述
编写dynamicprops
的子类使我可以向对象动态添加属性:
Writing a subclass of dynamicprops
allows to me to add properties dynamically to an object:
addprop(obj, 'new_prop')
这很棒,但是我也很想为这些属性即时创建set / get
函数.或适用于这些动态属性的分析功能.
This is great, but I would also love to create set / get
functions for these properties on the fly. Or analysis functions that work on these dynamic properties.
到目前为止,我对Matlab的经验是,一旦我创建了一个类的实例,就不可能添加新的方法.这非常麻烦,因为我的对象可能包含很多数据,每次我想添加新方法时都必须重新加载这些数据(因为必须执行clear classes
).
My experience with Matlab has been so far, that once I create an instance of a class, adding new methods is not possible. That is very cumbersome, because my object may contain a lot of data, which I'll have to re-load every time that I want to add a new method (because I have to do clear classes
).
那么有没有一种动态添加方法的方法?
So is there a way to add methods on the fly?
推荐答案
您不能像添加动态属性那样添加方法.但是,有两种方法可以在开发过程中实施新方法,而无需每次都重新加载数据.
You cannot add methods like you add dynamic properties. However, there are two ways for implementing new methods during development that won't require you to re-load the data every time.
(1)我将标准方法编写为单独的函数,并在开发过程中将其称为myMethod(obj)
.一旦确定它们是稳定的,就将它们的签名添加到类定义文件中-当然,这需要一个clear classes
,但这是一个大大的延迟,并且您可能会不时地关闭Matlab ,无论如何.
(1) I write standard methods as separate functions, and call them as myMethod(obj)
during development. Once I'm sure they're stable, I add their signature into the class definition file - this requires a clear classes
, of course, but it is a much delayed one, and from time to time you may have to shut down Matlab, anyway.
(2)使用set/get方法,事情有些棘手.如果使用dynamicprops
添加新属性,则还可以指定其set/get方法(但是,这些方法/函数很可能希望接收属性名称,以便它们知道要引用的内容):
(2) With set/get methods, things are a little trickier. If you are using dynamicprops
to add new properties, you can also specify their set/get methods, however (most likely, these methods/functions will want to receive the name of the property so that they know what to refer to):
addprop(obj,'new_prop');
prop = findprop(obj,'new_prop');
prop.SetMethod = @(obj,val)yourCustomSetMethod(obj,val,'new_prop')
编辑
(2.1)这是一个示例,说明如何设置隐藏属性以存储和检索结果(基于 jmlopez的答案).显然,如果您对自己的实际设计有个更好的了解,可以大大改善这一点
(2.1) Here's an example of how to set up a hidden property to store and retrieve results (based on jmlopez' answer). Obviously this can be improved a lot if you have a better idea what you're actually designing
classdef myDynamicClass < dynamicprops
properties (Hidden)
name %# class name
store %# structure that stores the values of the dynamic properties
end
methods
function self = myDynamicClass(clsname, varargin)
% self = myDynamicClass(clsname, propname, type)
% here type is a handle to a basic datatype.
self.name_ = clsname;
for i=1:2:length(varargin)
key = varargin{i};
addprop(self, key);
prop = findprop(self, key);
prop.SetMethod = @(obj,val)myDynamicClass.setMethod(obj,val,key);
prop.GetMethod = @(obj)myDynamicClass.getMethod(obj,key);
end
end
function out = classname(self)
out = self.name_;
end
end
methods (Static, Hidden) %# you may want to put these in a separate fcn instead
function setMethod(self,val,key)
%# have a generic test, for example, force nonempty double
validateattributes(val,{'double'},{'nonempty'}); %# will error if not double or if empty
%# store
self.store.(key) = val;
end
function val = getMethod(self,key)
%# check whether the property exists already, return NaN otherwise
%# could also use this to load from file if the data is not supposed to be loaded on construction
if isfield(self.store,key)
val = self.store.(key);
else
val = NaN;
end
end
end
end
这篇关于是否可以即时将方法添加到MATLAB类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!