【问题标题】:How to access variables in the properties block of a Matlab System Object?如何访问 Matlab 系统对象的属性块中的变量?
【发布时间】:2015-09-07 21:07:06
【问题描述】:

我正在使用 Matlab/Simulink 开发一个简单的系统对象。

看起来像这样:

classdef realtime_header_detectorSO < matlab.System & matlab.system.mixin.Propagates
    % correlateHeader
    %
    % This template includes the minimum set of functions required
    % to define a System object with discrete state.

    properties
       Header
       %nrOfBitsInPreviousStep=0;
       s=100;
       d=zeros(1,s);



    end

    properties (DiscreteState)

    end

    properties (Access = private)
              max_nr_of_packets=20;
      max_packet_length_in_bytes=300;
      current_packet=1;
        % Pre-computed constants.
     %   h = commsrc.pn('GenPoly', [9 5 0],'NumBitsOut', 8,'InitialStates',ones(1,9));
  %   data=logical(zeros(max_nr_of_packets,max_packet_length_in_bytes*8));
    end

    methods (Access = protected)
        function setupImpl(obj,u)
            % Implement tasks that need to be performed only once, 
            % such as pre-computed constants.

        end

        function [maxCorr]= stepImpl(obj,u)
            eml.extrinsic('num2str');
             coder.extrinsic('sprintf');
            % Implement algorithm. Calculate y as a function of
            % input u and discrete states.
            %y = size(u,1);

                symbols=sign(u);
                c=abs(conv(flipud(obj.Header),[symbols; symbols]));

                maxCorr=max(c);    
              %  maxCorr
                if(maxCorr==36)

                    idx36=find(c(1:size(symbols,1))==36);
                    disp('header(s) detected at the following location(s) in bytes:');
                    disp(sprintf('%15.4f \n',idx36/8));
                    nrOfSymbols=size(symbols,1);
                    disp(['out of nr. of total symbols: ' num2str(nrOfSymbols)]);
                    disp('------');
                %    maxCorr
                end
            % y=obj.pBufferIdx;
        end

        function resetImpl(obj)
            % Initialize discrete-state properties.
        end

        function varargout = isOutputFixedSizeImpl(~)
             varargout = {true};
        end

        function varargout = getOutputSizeImpl(obj)
           varargout = {[1 1]};
        end
    end

end

但是当我编译/运行它时,我得到以下错误:

The System object name 'realtime_header_detectorSO' specified in MATLAB System block 'freqScanningRT/Sync and
Find Header/detect header' is invalid.

Caused by:
    Undefined function or variable 's'.

但是 (!) 以下代码编译并运行良好:

classdef realtime_header_detectorSO < matlab.System & matlab.system.mixin.Propagates
    % correlateHeader
    %
    % This template includes the minimum set of functions required
    % to define a System object with discrete state.

    properties
       Header
       %nrOfBitsInPreviousStep=0;
       s=100;
  %     d=zeros(1,s);



    end

    properties (DiscreteState)

    end

    properties (Access = private)
              max_nr_of_packets=20;
      max_packet_length_in_bytes=300;
      current_packet=1;
        % Pre-computed constants.
     %   h = commsrc.pn('GenPoly', [9 5 0],'NumBitsOut', 8,'InitialStates',ones(1,9));
  %   data=logical(zeros(max_nr_of_packets,max_packet_length_in_bytes*8));
    end

    methods (Access = protected)
        function setupImpl(obj,u)
            % Implement tasks that need to be performed only once, 
            % such as pre-computed constants.

        end

        function [maxCorr]= stepImpl(obj,u)
            eml.extrinsic('num2str');
             coder.extrinsic('sprintf');
            % Implement algorithm. Calculate y as a function of
            % input u and discrete states.
            %y = size(u,1);
                disp(obj.s);
                symbols=sign(u);
                c=abs(conv(flipud(obj.Header),[symbols; symbols]));

                maxCorr=max(c);    
              %  maxCorr
                if(maxCorr==36)

                    idx36=find(c(1:size(symbols,1))==36);
                    disp('header(s) detected at the following location(s) in bytes:');
                    disp(sprintf('%15.4f \n',idx36/8));
                    nrOfSymbols=size(symbols,1);
                    disp(['out of nr. of total symbols: ' num2str(nrOfSymbols)]);
                    disp('------');
                %    maxCorr
                end
            % y=obj.pBufferIdx;
        end

        function resetImpl(obj)
            % Initialize discrete-state properties.
        end

        function varargout = isOutputFixedSizeImpl(~)
             varargout = {true};
        end

        function varargout = getOutputSizeImpl(obj)
           varargout = {[1 1]};
        end
    end

end

所以我可以在stepImpl(obj,u) 中以obj.s 访问s,但我无法在定义它的属性块内访问s

现在这令人困惑。

有没有办法在属性块内访问s

问题是我必须使用属性块,因为如果我尝试这个:

 function setupImpl(obj,u)
        % Implement tasks that need to be performed only once, 
        % such as pre-computed constants.
        d=zeros(1,obj.s);

  end

然后我得到:

Error due to multiple causes.

Caused by:
    Problem creating simulation target MEX-file for model 'freqScanningRT'.
    Simulink detected an  error
     'Computed maximum size is not bounded.
    Static memory allocation requires all sizes to be bounded.
    The computed size is [1 x :?].'.

     The error occurred for MATLAB System block 'freqScanningRT/Sync and Find Header/detect header'. See line
     34, column 15 in file
     'path/realtime_header_detectorSO.m'.
     The error was detected during code generation phase.
     Start code generation report.

     To prevent this error, use one of the following:
     * Modify the System object to avoid code that does not support code generation.
     * Change 'Simulate using' parameter to 'Interpreted Execution'.

知道如何引用properties 块中的变量吗?

一定有办法做到这一点。

【问题讨论】:

    标签: matlab simulink


    【解决方案1】:

    你应该可以使用

    properties
           s = 100;
           d = zeros(1,100);
    end
    

    对吗?如果您已经将 100 作为 s 的默认值,您还应该能够将其作为 d 的默认值的一部分。

    我猜您正试图避免这样做,因为重复“100”会让您感到不舒服。但我也猜想,“100”是你的系统所依赖的某种神奇数字。所以真的,你应该试着把它作为一个常数在任何情况下,无论它是否重复。

    所以在这种情况下,你可能会改进

    properties (Constant)
        MYCONSTANT = 100;
    end
    
    properties
           % Reference Constant class properties with the class name
           s = realtime_header_detectorSO.MYCONSTANT;
           d = zeros(1, realtime_header_detectorSO.MYCONSTANT);
    end
    

    您将无法执行您最初尝试执行的操作 - 在定义另一个属性时,无法在属性定义块中引用一个属性的名称(即使您可以很好地引用它在一个方法内)。我想我理解您为什么会感到困惑 - 但为了消除您的困惑,请注意您无法保证 MATLAB 实例化属性默认值的 order。如果它在创建s 之前尝试创建d,它显然会失败。

    【讨论】:

    • 这看起来正是我正在寻找的答案!非常感谢 !我明天在工作中尝试这个解决方案。
    【解决方案2】:

    你可以通过在构造函数中初始化属性来避免这个问题。

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 1970-01-01
      • 2018-10-21
      • 2021-05-22
      • 2019-10-18
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 2012-06-30
      相关资源
      最近更新 更多