如何访问Matlab系统对象属性块中的变量?

如何访问Matlab系统对象属性块中的变量?,matlab,simulink,Matlab,Simulink,我正在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 Syste

我正在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
但是(!)以下代码编译并运行良好:

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
然后我得到:

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
       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实例化属性默认值的顺序。如果它在创建
s
之前尝试创建
d
,显然会失败。

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

这看起来正是我想要的答案!非常感谢!我明天在工作中尝试这个解决方案。
properties
       s = 100;
       d = zeros(1,100);
end
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