save n-Numbers of last interation from a loop

1 view (last 30 days)
hello
i want to save always the last n (n between 1 and 6) interation of a loop.
i have a regulator which is read out in a loop. now i need always the last n inerations of the loop to calculate the desirerd condition.
function ReglerButtonPushed(app, event)
k= app.KWertSlider.Value;
bereich= app.BereichSlider.Value;
speed= app.SpeedSlider.Value;
fehlervek= zeros(6,1);
l
app.stopRegler = false;
while app.stopRegler == false
b = getBrightness(app,app.sensorPos(1),app.sensorPos(2));
app.HelligkeitEditField.Value=b;
fehler = b - 0.5; %actual mistake
i know how to save all mistakes - i just need the actually last n interations.

Accepted Answer

Jan
Jan on 28 Nov 2018
n = 6;
fehlerVec = nan(1, n);
while app.stopRegler == false
b = getBrightness(app,app.sensorPos(1),app.sensorPos(2));
app.HelligkeitEditField.Value=b;
fehler = b - 0.5; %actual mistake
fehlerVec = [fehler(2:n), fehler];
end
Now fehlerVec contains the last n elements. There are more efficient solutions, but collecting these n values is most likely not the time-crticial part.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!