Azzera filtri
Azzera filtri

Implementing a shift register into a Matlab function

17 visualizzazioni (ultimi 30 giorni)
Hello everyone,
I'm trying to write a shift register in a MATLAB-Function in Simulink. So I have an initial Matrix M, that is supposed to change for every new Input-value of L in Simulink in a way that it saves the new input value as it's first value and pushes the other values one step back and deletes the last value.
For better explanation: If this wasn't a function, but a calculation in a loop, the working code would look like this:
M=[1; 2; 3; 4; 5];
L=[10;20;30;40;50];
for x=1:5
M=[L(x); M(1:end-1)];
end
However I want to use real- time data in the end, so this doesn't work for me.
This is what I have so far (it's a Matlab-Function in Simulink):
function y = fcn(L, M)
M=[L ; M(1:end-1)];
y = mean(M);
When I run this, it just overwrites the first value in M and doesn't push the other values back. Can anybody help?

Risposta accettata

Ameer Hamza
Ameer Hamza il 30 Nov 2020
To save a variable between different iterations of MATLAB function block in Simulink, declare it as persistent. For example, if the initial value of M is given, you can do something like this
function y = fcn(L)
persistent M
if isempty(M)
M = [1; 2; 3; 4; 5];
end
M = [L; M(1:end-1)];
y = mean(M);
  4 Commenti
AN
AN il 1 Dic 2020
Modificato: AN il 1 Dic 2020
All right, this works perfectly. Thank you so much for your help!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Simulink Environment Customization in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by