解决从超类继承时实现 matlab.system 的问题
Troubleshoot with implementing matlab.system from when inheriting from superclass
我正在尝试将 matlab class 实现到 simulink MATLABSystem 中。当我实现 superclass 时,模拟工作正常,但是使用 subclass 我得到以下错误:
》Simulink无法自动推断[=27=的输出信号属性]。Simulink使用代码生成技术从System object中自动判断输出信号属性。System object 'subclass' 包含不支持代码生成的代码。
错误是
'未定义函数或变量 'obj'。对局部变量的第一次赋值决定了它的 class。'
MATLAB System 模块 'subclassSystem' 发生错误。请参阅文件 'superclass.m' 中的第 29 行第 28 列。在大小传播阶段检测到错误。"
我在下面的代码中注释了这一行
我是否必须指定其他内容?
这里是子class定义:
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
这里是超级class定义:
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 函数中,我不得不以 obj 作为参数调用超类的 setupImpl:
setupImpl@Superclass(obj);
这是我的构造函数。这里我没有使用obj作为return值
function obj = Subclass()
obj = obj@Superclass();
end
我正在尝试将 matlab class 实现到 simulink MATLABSystem 中。当我实现 superclass 时,模拟工作正常,但是使用 subclass 我得到以下错误:
》Simulink无法自动推断[=27=的输出信号属性]。Simulink使用代码生成技术从System object中自动判断输出信号属性。System object 'subclass' 包含不支持代码生成的代码。 错误是 '未定义函数或变量 'obj'。对局部变量的第一次赋值决定了它的 class。' MATLAB System 模块 'subclassSystem' 发生错误。请参阅文件 'superclass.m' 中的第 29 行第 28 列。在大小传播阶段检测到错误。"
我在下面的代码中注释了这一行
我是否必须指定其他内容?
这里是子class定义:
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
这里是超级class定义:
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 函数中,我不得不以 obj 作为参数调用超类的 setupImpl:
setupImpl@Superclass(obj);
这是我的构造函数。这里我没有使用obj作为return值
function obj = Subclass()
obj = obj@Superclass();
end