全局变量未在2级s函数中初始化

时间:2016-08-24 09:53:21

标签: matlab simulink mex s-function

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

export default function applyMiddleware(...middlewares) {
  return (createStore) => (reducer, preloadedState, enhancer) => {
    var store = createStore(reducer, preloadedState, enhancer)
    var dispatch = store.dispatch // this is the original dispatch
    var chain = []

    /*** this the store param of the middleware ***/
    var middlewareAPI = {
      getState: store.getState,
      dispatch: (action) => dispatch(action) 
    }

    /*** store param is applied to old middlewares to create the chain ***/
    chain = middlewares.map(middleware => middleware(middlewareAPI))

    /*** The chain is composed. For all methods in the chain, but the last, next() is the middleware method after it in the chain, the last next is store.dispatch ***/
    dispatch = compose(...chain)(store.dispatch)

    return {
      ...store, 
      dispatch // the new dispatch is returned instead of the old
    }
  }
}

现在的问题是虽然我已将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) 声明为全局变量,但当我运行该函数时,它表示我未定义,是否有人知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

function Update(block)

global i

% ...... 

中也有错误
block.OutputPort(1).Data(i) = block.InputPort(1).Data;

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