我正在尝试将 matlab 类实现到 simulink MATLABSystem 中。当我实现超类时,模拟工作正常,但是使用子类时,我收到以下错误:
"Simulink 无法自动推断'子类系统'的输出信号属性。Simulink 使用代码生成技术自动确定系统对象的输出信号属性。 系统对象"子类"包含不支持代码生成的代码。 错误是 '未定义的函数或变量'obj'。对局部变量的第一个赋值决定了它的类。 MATLAB 系统块"子类系统"发生错误。请参阅文件 '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
我发现了错误。在子类的 setupImpl 函数中,我不得不调用超类的 setupImpl,并将 obj 作为参数:
setupImpl@Superclass(obj);
这是我的构造函数。在这里我没有使用 obj 作为返回值
function obj = Subclass()
obj = obj@Superclass();
end