One way to quickly implement a circular buffer is to use modulus to circle back around to the front. This will slightly modify the order of the data from what you specified but may be faster and equivalent if you simply replace the oldest data with the newest so instead of
[B2 B3 B4 B5 B6]
You get
[B6 B2 B3 B4 B5]
By using code like this:
bufferSize = 5;
data = nan(bufferSize,1)';
for ind = 1:bufferSize+2
data(mod(ind-1, bufferSize)+1) = ind
end
and this works for arbitrary sized data.
If you're not familiar with modulo, the mod
function returns effectively the remainder of a division operation. So mod(3,5)
returns3
, mod(6,5)
returns 1
, mod(7,5)
returns 2
and so on until you reach mod(10,5)
which equals 0 again. This allows us to 'wrap around' the vector by moving back to the start every time we reach the end. The +1
and -1
in the code is because MATLAB starts its vector indices at 1 rather than 0 so to get the math to work out right you have to remove 1 before doing the mod
then add it back in to get the right index. The result is that when you try and write the 6th element to your vector is goes and writes it to the 1st position in the vector.