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!