Persistent variable buffer size
1 回表示 (過去 30 日間)
古いコメントを表示
I need to make a persistent variable that can take maximum x scalars. After x is reached the oldest scalar stored gets out and the new one gets in (FIFO).
How can I define such variable?
0 件のコメント
回答 (1 件)
Jan
2015 年 4 月 26 日
What have you tried and which problems occur?
function YourFcn(Value)
% Initialize the buffer on demand:
persistent Buffer BufferIndex
MaxBufferLen = 19;
if isempty(BufferIndex) % First run
BufferIndex = 1;
% Pre-allocation if possible: Buffer = nan(1, MaxBufferLen)
end
% Store the current value:
Buffer(BufferIndex) = Value;
% Increase and restart the index:
BufferIndex = mod(BufferIndex, MaxBufferLen) + 1
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!