我正在尝试在simulink MATLABSystem中实现matlab类。当我实现超类时,模拟工作正常,但是使用子类时出现以下错误:
“ Simulink无法自动推断'subclassSystem'的输出信号属性。Simulink使用代码生成技术从System对象自动确定输出信号属性。系统对象'subclass'包含的代码不支持代码代。 错误是 '未定义的函数或变量'obj'。对局部变量的第一个赋值决定了其类别。 MATLAB System块“ subclassSystem”发生了错误。参见文件“ superclass.m”中的第29行第28列。在尺寸传播阶段检测到错误。”
我在下面的代码中注释了这一行
我还必须指定其他内容吗?
这是子类定义:
classdef subclass < superclass
properties(Access = protected) % These variables must be initialised. Here or in the setupImpl function
end
methods (Access=protected)
function resetImpl(obj)
end
%% Common functions
function setupImpl(obj)
% Perform one-time calculations, such as computing constants
setupImpl@superclass();
%obj.initFilter(obj.sampleTime, obj.delta_i, obj.delta_d, obj.g_mps2, obj.q0, obj.b_w0, obj.sigma_2_w, obj.sigma_2_a, obj.sigma_2_b_w, obj.sigma_2_yaw)
end
function attitude = stepImpl(obj,u, y)
% Implement algorithm.
attitude = 5;
end
end
methods
% Constructor must be empty for matlab.System. In Matlab call
% initFilter after the object was created. In simulink setupImpl()
% will be called
function obj = subclass()
obj@superclass();
end
end
end
以下是超类定义:
classdef superclass < matlab.System
% These variables must be initialized in the simulink model
properties
sigma_2_w;
sigma_2_a;
sigma_2_b_w;
sigma_2_yaw;
end
properties(Access = protected) % These variables must be initialised. Here or in the setupImpl function
R;
Q;
end
methods (Access = protected)
function resetImpl(obj)
end
%% Common functions
function setupImpl(obj)
% Perform one-time calculations, such as computing constants
obj.Q = diag([obj.sigma_2_w',obj.sigma_2_b_w']); % this is line 29
obj.R = diag([obj.sigma_2_a',obj.sigma_2_yaw']);
end
function attitude = stepImpl(obj,u, y)
% Implement algorithm.
attitude = 5;
end
end
methods
% Constructor must be empty for matlab.System. In Matlab call
% initFilter after the object was created. In simulink setupImpl()
% will be called
function obj = superclass()
% Support name-value pair arguments when constructing object
end
end
end
答案 0 :(得分:0)
我发现了错误。在子类的setupImpl函数中,我必须以obj作为参数调用超类的setupImpl:
setupImpl@Superclass(obj);
这是我的构造函数。在这里,我没有使用obj作为返回值
function obj = Subclass()
obj = obj@Superclass();
end