我试图运行一个OOP程序的八度,最初为Matlab编写。我不能得到大部分的部分正确,但不能调用方法。
这是示例代码。
obj = Motor('SamplePeriod', 1/256, 'Beta');
methods(obj)
Methods for class Motor:
Motor Update
obj.Update();
error: invalid index for class我试着看了说明书,没能拿到说明书。此外,网上也没有任何样品。
发布于 2014-05-23 13:51:33
正如所描述的这里,您需要使用替代方式调用您的方法:
m = motor(...)
p = power(m, ...) 在你的情况下
Update(obj) 正如丹尼尔所提到的
发布于 2016-07-15 08:57:36
仍然可以使用obj.Update()语法调用类的方法,但需要在类中实现一个特殊方法:
function varargout = subsref (obj, idx)
persistent __method__ method4field typeNotImplemented
if isempty(__method__)
__method__ = struct();
__method__.Update = @(o,varargin) Update (o, varargin{:});
# Error strings
method4field = "Class #s has no field #s. Use #s() for the method.";
typeNotImplemented = "#s no implemented for class #s.";
end
method = idx(1).subs;
if ~isfield(__method__, method)
error('Unknown method #s.',method);
else
fhandle = __method__.(method);
end
if strcmp (idx(2).type, '()')
args = idx(2).subs;
if isempty(args)
out = fhandle (obj);
else
out = fhandle (obj, args{:});
end
varargout{1} = out;
end
endfunction更多细节可以在几何学包中找到,查看inst/io/@svg文件夹。
https://stackoverflow.com/questions/23830523
复制相似问题