Storing input variables x in a persistent variable called buffer
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
yashar khatib shahidi
il 2 Mag 2015
Modificato: yashar khatib shahidi
il 4 Mag 2015
Hi there,
I am trying yo create a function which input x as a scalar and stores in a persistent variable called buffer. and it calculates the average of input every time I input a new x. The function uses a “buffer” to hold previous inputs, and the buffer can hold a maximum of 25 inputs. I want to know how I can add input to the buffer which is a persistent variable and see the inputs stored in the buffer? Thanks
0 Commenti
Risposta accettata
Jan
il 2 Mag 2015
The description sounds clean and clear enough to allow for a straight forward implementation. What should happen if the function is called the 26th time? Should the buffer wrap around in a first-in-first-out style?
What have you tried so far? Usually it is more efficient to posr, what you have written and ask specific questions. But it is saturday and I try it:
function YourAccumulator(x)
persistent buffer bufferIndex
if isempty(buffer) % Called the first time
bufferIndex = 0;
end
bufferIndex = bufferIndex + 1
buffer(bufferIndex) = x;
if bufferIndex == 25
bufferIndex = 1;
end
M = mean(buffer);
disp(M);
The mod() operator would help to remove the if part.
1 Commento
yashar khatib shahidi
il 4 Mag 2015
Modificato: yashar khatib shahidi
il 4 Mag 2015
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Language Fundamentals in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!