1

我正在尝试制作一个类似于缓冲区的 2 级 s-function(我刚开始学习 S-functions)。现在我想要的是,每次输入进入时,它都会存储在下一个索引中,直到缓冲区变满,然后它开始将存储的数据从第 2 个到最后一个索引推送到第一个到第二个索引,并在采样时间后自我更新我将附上我的代码,以便更好地理解

function Buffer(block)
global i;
i = 1;
setup(block);

function setup(block)

% Register number of ports
block.NumInputPorts  = 2;
block.NumOutputPorts = 1;

% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;

% Override input port properties
block.InputPort(2).Dimensions        = 1;
block.InputPort(2).DatatypeID  = 0;  % double
block.InputPort(2).Complexity  = 'Real';
block.InputPort(2).DirectFeedthrough = true;

% Override input port properties
block.InputPort(1).Dimensions        = 1;
block.InputPort(1).DatatypeID  = 0;  % double
block.InputPort(1).Complexity  = 'Real';
block.InputPort(1).DirectFeedthrough = true;



% Override output port properties
block.OutputPort(1).Dimensions       = [1 block.InputPort(2).Data];
block.OutputPort(1).DatatypeID  = 0; % double
block.OutputPort(1).Complexity  = 'Real';
block.SampleTimes = [-1 0];

block.SimStateCompliance = 'DefaultSimState';


block.RegBlockMethod('Outputs', @Outputs);    
block.RegBlockMethod('Update', @Update);

block.RegBlockMethod('Terminate', @Terminate); 

function Outputs(block)

block.OutputPort(1).Data(i) = block.InputPort(1).Data;
% block.Dwork(1).Data
%end Outputs

%%
%% Update:
%%   Functionality    : Called to update discrete states
%%                      during simulation step
%%   Required         : No
%%   C-MEX counterpart: mdlUpdate
%%
function Update(block)



if(i == block.InputPort(2).Data)
    block.OutputPort(1).Data(1:block.InputPort(2).Data - 1) = block.OutputPort(1).Data(2:block.InputPort(2).Data);
else
    i = i + 1;    
end
%end Update




function Terminate(block)

现在的问题是,虽然我已经声明i为全局变量,但当我运行它说 i 未定义的函数时,有人知道我在做什么错吗?

4

1 回答 1

0

i必须在每个使用它的函数中声明为全局的:

function Update(block)

global i

% ...... 

线路也有错误

block.OutputPort(1).Data(i) = block.InputPort(1).Data;

这是因为block.InputPort(2).Datainsetup是空的并且block.OutputPort(1).Data有 size 1。在 s-block 参数对话框而不是在块的第二个输入中设置缓冲区大小可能会更好吗?

于 2016-08-24T12:19:36.783 回答