【问题标题】:Troubleshoot with implementing matlab.system from when inheriting from superclass从超类继承时实现 matlab.system 的疑难解答
【发布时间】:2019-09-03 11:00:42
【问题描述】:

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

“Simulink 无法自动推断‘subclassSystem’的输出信号属性。Simulink 使用代码生成技术从System 对象自动确定输出信号属性。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

【问题讨论】:

  • 如果根本没有构造函数会怎样?

标签: matlab code-generation simulink


【解决方案1】:

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

setupImpl@Superclass(obj);

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多