save n-Numbers of last interation from a loop

1 次查看(过去 30 天)
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.

采纳的回答

Jan
Jan 2018-11-28
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by