Stop updating during a FOR loop

4 次查看(过去 30 天)
Jaffatron
Jaffatron 2016-1-7
编辑: jgg 2016-1-7
I have a question regarding a FOR loop.
I'm looking to calculate 2 values (and their control limits) using a FOR loop over time from a variable, F, in a moving window. I've 2 functions, 1 to calculate the values and another to calculate the control limits. And when the value is above it's control limit for 4 consecutive values a true fault is detected. What I'm looking to do is for when a true fault occurs for a point of time after I want to hold the limits to flush out all the faulty data before updating my model again
My code so far is:
NOC1 = Get_Control(F); %%get initial control limit
for i = 1:(size(F,1))
abx = F(i,:); %%add the new value each time
MRSS= Get_Values(abx,NOC1); %%Works out the values of Q and T
%Calculate Q
Q_new = MRSS.Q;
Q = [Q; Q_new];
QLimit= [QLimit; QLimit_new];
% Calculate T
T_new= MRSS.T;
T= [T; T_new];
TLimit = [T2cfdLimit; T2cfdLimit_new];
% Update the model
abx = abx(2:end,:); %%takes away oldest value
if i > 5 && Q(i) > QLimit(i) && all(Q(i-4:i) > QLimit(i-4:i))
QLimit_new = QLimit(end);
TLimit_new = TLimit(end); %%therefore not updating the limits during the fault
else
% Calculate new limits
NOC1 = Get_Control(abx);
QLimit_new = NOC1.QLimit;
TLimit_new = NOC1.TLimit;
end
end
Basically what I'm looking for is a way to stop the control limits updating for a certain length of time to flush out all the faulty data. I've a feeling I have to put a for loop in after the if statement however this won't stop the updating;
I was thinking something like:
if i > 5 && Q(i) > QLimit(i) && all(Q(i-4:i) > QLimit(i-4:i))
for j = i:(i+Xwindow)
QLimit_new = QLimit(end);
TLimit_new = TLimit(end); %%therefore not updating the limits during the fault
end
end

回答(1 个)

jgg
jgg 2016-1-7
编辑:jgg 2016-1-7
I think I would suggest something like this:
NOC1 = Get_Control(F); %%get initial control limit
fault_state = 0;
for i = 1:(size(F,1))
abx = F(i,:); %%add the new value each time
MRSS= Get_Values(abx,NOC1); %%Works out the values of Q and T
%Calculate Q
Q_new = MRSS.Q;
Q = [Q; Q_new];
QLimit= [QLimit; QLimit_new];
% Calculate T
T_new= MRSS.T;
T= [T; T_new];
TLimit = [T2cfdLimit; T2cfdLimit_new];
% Update the model
abx = abx(2:end,:); %%takes away oldest value
if i > 5 && Q(i) > QLimit(i) && all(Q(i-4:i) > QLimit(i-4:i)) && fault_state == 0 %new fault detected
fault_state = Xwindow; %if a fault is detected, start a countdown
end
if fault_state > 0
QLimit_new = QLimit(end);
TLimit_new = TLimit(end); %%therefore not updating the limits during the fault
fault_state = fault_state - 1;
else
% Calculate new limits
NOC1 = Get_Control(abx);
QLimit_new = NOC1.QLimit;
TLimit_new = NOC1.TLimit;
end
end
Basically, the idea is to create a countdown timer. When you detect an error, start the timer. Only update the values when the timer is zero, otherwise hold on the old values. I can't debug this since I don't have your code, but I think this should be roughly what you want to do.

类别

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