While loop not working
Show older comments
Hi
I need help with creating a while loop that works. I have SCADA data which I need to average from 6 sensors, taken at 5 minute intervals. I need to calculate the average of all 6 sensors, find the deviation of each entry from the average and then "discard" values greater than 5%. This calculation needs to continue until none of the entries have a deviation greater than 5% from the average. So far I have come up with the code below, it doesn't seem to be working and I believe it has to do with my condition for starting and ending the while loop. Please help
%Average Pyr Irradiance for the 6 selected pyranometers at Kathu PV
%Clear variables
clear counter;
clear i;
clear j;
%Average data from 5-miute to 15-minute intervals
%Make sure correct monthly data is being analysed
PyrData=zeros(size(Pyr_Mar16,1)/3, size(Pyr_Mar16,2));
counter = 1;
for i=1:3:size(Pyr_Mar16,1)
for j=1:size(Pyr_Mar16,2)
K=Pyr_Mar16(i:i+3-1,j);
PyrData(counter,j)=mean(K(~isnan(K))); %take the average of all the non "Not-A-Number" values
end
counter=counter + 1;
end
%Round down any values less than 1 to 0 so that negative and close-to-zero
%numbers are removed
for i=1:size(PyrData,1)
for j=1:size(PyrData,2)
if PyrData(i,j)<0.1
PyrData(i,j)=0;
end
end
end
% Total Average for all pyranometers
RealPyrAvg=zeros(size(PyrData,1),1);
for i=1:size(PyrData,1)
K=PyrData(i,1:size(PyrData,2));
RealPyrAvg(i,1) =mean(K(~isnan(K)));
end
%Check Pyranometer Irradiation Deviation and
checkPyrDEV=zeros(size(PyrData,1),size(PyrData,2));
FiveperThresh=zeros(size(PyrData,1),size(PyrData,2));
for i=1:size(PyrData,1)
countcol=0;
Z=1;
for j=1:size(PyrData,2)
while Z==1
countcol=countcol+1;
%Step 1: Check deviation percentage
if ~isnan(PyrData(i,j))
checkPyrDEV(i,j)=(abs(PyrData(i,j)-RealPyrAvg(i,1))/RealPyrAvg(i,1))*100;
else
checkPyrDEV(i,j)=NaN;
end
%Step 2: Flag as 1 if > 5% and 0 if equal to or less than 5%
if checkPyrDEV(i,j)>5
FiveperThresh(i,j)=1;
else
FiveperThresh(i,j)=0;
end
if countcol==size(PyrData,2)
if sum(FiveperThresh(i,:))>0
A=max(checkPyrDEV(i,:));
B=find(checkPyrDEV(i,:)==A);
PyrData(i,B)=NaN;
K=PyrData(i,1:size(PyrData,2));
RealPyrAvg(i,1) =mean(K(~isnan(K)));
elseif sum(FiveperThresh(i,:))==0
Z=0;
end
end
end
end
end
%Corrected Average
CorrAvgPyr=zeros(size(PyrData,1),1);
for i=1:size(PyrData,1)
CorrAvgPyr(i,1)=RealPyrAvg(i,1);
end
Answers (0)
Categories
Find more on System Identification Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!