How can I implement a rolling list ?

조회 수: 5 (최근 30일)
Blake
Blake 2012년 9월 30일
I am reading data over the network in real time and I want to limit a dataset to 1000 points. For example when the array has filled 1000 points I want it to start at 1 again to save memory. I also want to ensure I keep the order so that when i plot the information it will plot oldest to newest and not in the way it was stored.
Is there any such feature in MATLAB?

채택된 답변

Walter Roberson
Walter Roberson 2012년 9월 30일
You could also consider
if length(data) == 1000
data(1) = [];
end
data(end+1) = theNewDataValue;

추가 답변 (3개)

Image Analyst
Image Analyst 2012년 9월 30일
Something like this perhaps:
% Assign new data to the next element.
data(nextIndexToUse) = theNewDataValue;
% Increment to the next element to use.
nextIndexToUse = nextIndexToUse + 1;
% Reset back to 1 if it exceeds 1000
if nextIndexToUse > 1000
nextIndexToUse = 1;
end

Malcolm Lidierth
Malcolm Lidierth 2012년 9월 30일
편집: Malcolm Lidierth 2012년 9월 30일
or
Edited for unity-based indexing as per @Blake's remark below:
x=zeros(1,1000)
circindex=@(k)mod(k-1,numel(x))+1
x(circindex(100001))=1

Blake
Blake 2012년 9월 30일
편집: Blake 2012년 9월 30일
I like the circular index idea. I will note this for later but unfortunately it doesnt arrange the data in an easy to plot format. It is difficult to differentiate from new and old points just by looking at the list. Also there is a time at the end of the array where the mod(1000,1000) will try to access x(0). But it implements a rolling list perfectly other than that. Thank you.
Walter's code implements it exactly how I wanted it. Thanks
  댓글 수: 1
Malcolm Lidierth
Malcolm Lidierth 2012년 9월 30일
@Blake
I forgot the unity-base in MATLAB. Code corrected above. To extract data with N samples:
x([circindex(N):-1:1 end:-1:circindex(N)+1])
Extra code for N<=0 not included.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by