从超类继承时实现matlab.system的疑难解答
我正在尝试将一个matlab类实现到simulink matlab系统中。当我实现超类时,模拟工作正常,但是使用子类我得到以下错误: “Simulink无法自动推断“子类系统”的输出信号属性。Simulink使用代码生成技术自动确定系统对象的输出信号属性。系统对象“子类”包含不支持代码生成的代码。 错误是 '未定义的函数或变量'obj'。局部变量的第一个赋值决定其类别。' MATLAB系统块“Subclass System”发生错误。请参阅文件“superclass.m”第29行第28列。该错误是在大小传播阶段检测到的。” 我在下面的代码中注释了这一行 我必须指定一些额外的内容吗 下面是子类定义:从超类继承时实现matlab.system的疑难解答,matlab,code-generation,simulink,Matlab,Code Generation,Simulink,我正在尝试将一个matlab类实现到simulink matlab系统中。当我实现超类时,模拟工作正常,但是使用子类我得到以下错误: “Simulink无法自动推断“子类系统”的输出信号属性。Simulink使用代码生成技术自动确定系统对象的输出信号属性。系统对象“子类”包含不支持代码生成的代码。 错误是 '未定义的函数或变量'obj'。局部变量的第一个赋值决定其类别。' MATLAB系统块“Subclass System”发生错误。请参阅文件“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
classdef子类
这里是超类定义:
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
classdef超类
我发现了错误。在子类的setupImpl函数中,我必须使用obj作为参数调用超类的setupImpl:
setupImpl@Superclass(obj);
这是我的构造函数。这里我没有使用obj作为返回值
function obj = Subclass()
obj = obj@Superclass();
end
我发现了错误。在子类的setupImpl函数中,我必须使用obj作为参数调用超类的setupImpl:
setupImpl@Superclass(obj);
这是我的构造函数。这里我没有使用obj作为返回值
function obj = Subclass()
obj = obj@Superclass();
end
如果你根本没有构造函数会发生什么?如果你根本没有构造函数会发生什么?