![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/539491/image.png)
Implementing a discrete buffer, i.e. a Memory Block with multiple frames
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Michael Sohnen
el 5 de Mzo. de 2021
Comentada: Michael Sohnen
el 5 de Mzo. de 2021
Dear Forum,
I really like the memory block in matlab to store a single value from the previous frame in a fixed-step discrete simulink simulation. However, is there any way to have a "memory block" with more than one sample of memory? For example, buffering the last 3 samples, and then being able to extract any of the three samples I want?
I have successfully done this using a persisent and preallocated matrix in a matlab function block. This is okay, bbut coding it is clunky, and it slows down my simulation. Is there any built-in solution?
Here is the matlab code that I would like a built in solution instead of:
%% matlab code inside a MatlabFunction block. REALLY SLOW!
%last in first out
function y=LIFO(u,bufferSize)
persistent buffer
if (isempty(buffer))
buffer=zeros(1,bufferSize)
end
y=buffer(1)
for i=1:bufferSize-1
buffer(i)=buffer(i+1)
end
buffer(bufferSize)=u
end
0 comentarios
Respuesta aceptada
Jonas
el 5 de Mzo. de 2021
Here is example code for storing the last 10 samples. Of course, you can adapt to memorize more history samples.
Summary: Use a Unit Delay block and feed it into a Selector block. The Selector block selects the (1:9) samples (so discarding the last one). Using a mux, we put these 9 samples at the end, and a new sample at the beginning. Then, the Unit Delay saves the 10 samples again to feed it back to the Selector.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/539491/image.png)
Please see attached model made in r2020b.
Más respuestas (0)
Ver también
Categorías
Más información sobre Programmatic Model Editing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!