从继承超类时实现matlab.system进行故障排除

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从继承超类时实现matlab.system进行故障排除相关的知识,希望对你有一定的参考价值。

我正在尝试将matlab类实现为simulink MATLABSystem。当我实现超类时,模拟工作正常,但使用子类我得到以下错误:

“Simulink无法自动推断'subclassSystem'的输出信号属性.Simulink使用代码生成技术自动确定System对象的输出信号属性.System对象'子类'包含不支持代码生成的代码。错误是'未定义的函数或变量'obj'。对局部变量的第一个赋值决定了它的类。 MATLAB系统块'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
答案

我发现了错误。在子类的setupImpl函数中,我不得不使用obj作为参数调用超类的setupImpl:

setupImpl@Superclass(obj);

这是我的构造函数。这里我没有使用obj作为返回值

function obj = Subclass()
    obj = obj@Superclass();
end

以上是关于从继承超类时实现matlab.system进行故障排除的主要内容,如果未能解决你的问题,请参考以下文章

继承基础

19.面向对象(继承)

将协议添加到超类,这将强制从其继承的其他类实现协议

swift 学习- 14 -- 继承

Python子类方法从超类方法继承装饰器

子类是不是从 Swift 中的超类扩展继承便利初始化程序?