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

时间:2015-06-22 12:03:03

标签: 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 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

因此,我可以s中的stepImpl(obj,u)访问obj.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

知道如何引用var io = require('socket.io').listen(1500); //for the socket io.set('origins', "http://localhost:130"); //the port of the application io.sockets.on('connection', function (socket) { // Let everyone know it's working socket.emit('startup', { message: 'I Am Working!!' }); }); 块中的变量吗?

必须有办法做到这一点。

2 个答案:

答案 0 :(得分: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

您无法完成您最初尝试的操作 - 在定义另一个属性时,无法在属性定义块中引用一个属性的名称(即使您可以很好地引用它在方法内)。我想我明白为什么你会发现这令人困惑 - 但要清除你的困惑,请注意你无法保证 order ,其中MATLAB实例化属性的默认值。如果它在创建d之前尝试创建s,则显然会失败。

答案 1 :(得分:0)

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

相关问题