Matlab 面向对象的八度编程
我正在尝试运行一个OOP程序,它是最初为Matlab编写的八度音阶。我不能得到大部分正确的部分,但不能调用方法 下面是示例代码Matlab 面向对象的八度编程,matlab,oop,octave,Matlab,Oop,Octave,我正在尝试运行一个OOP程序,它是最初为Matlab编写的八度音阶。我不能得到大部分正确的部分,但不能调用方法 下面是示例代码 obj = Motor('SamplePeriod', 1/256, 'Beta'); methods(obj) Methods for class Motor: Motor Update obj.Update(); error: invalid index for class 我试着阅读手册,但没有得到说明。而且网上也没有任何样品 如前所述,您需要使用另一种方
obj = Motor('SamplePeriod', 1/256, 'Beta');
methods(obj)
Methods for class Motor:
Motor Update
obj.Update();
error: invalid index for class
我试着阅读手册,但没有得到说明。而且网上也没有任何样品 如前所述,您需要使用另一种方法调用您的方法:
m = motor(...)
p = power(m, ...)
在你的情况下,那就是
Update(obj)
正如@Daniel所提到的,您仍然可以使用
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
文件夹。我想他可能需要obj=Update(obj)