MATLAB: While loop with if-statements to set criteria for data.

criteriasif statementwhile loop

Hello! I have a funktion that gets a data file as input. The function should then run through each line of the data file to check whether each line passes some criterias. If the line does not pass the criterias the fucntion should just skip that line and go to the next line without saving anything to the output variables. My problem is that even though a line is corrupted it is still saved in the output variable eventhough the function prints the "skipping line"-message. Can anybody please help me with this problem? Here is my function
function [W,D,t,C,filename] = load_data(filename)
fid = fopen(filename,'r');
i=0;
while feof(fid) == 0
i=i+1;
strLine = fgetl(fid);
numLine = str2num(strLine);
if numel(numLine)>1 && min(numLine(1))>=20 && max(numLine(1))<=200 && min(numLine(2))>=50 && max(numLine(2))<=500
if issorted(numLine(3:2:end)) && issorted(flip(numLine(4:2:end))) && length((numLine(4:2:end)))==length((numLine(3:2:end)))
if min(numLine(3:2:end))>=0 && max(numLine(3:2:end))<=360 && min(numLine(4:2:end))>=0 && max(numLine(4:2:end))<=50 && length(numLine(4:2:end))>=2
W(:,i) = numLine(1);
D(:,i) = numLine(2);
t{:,i} = numLine(3:2:end);
C{:,i} = numLine(4:2:end);
else
fprintf('Line nr %1.f is skipped\n',i)
end
else
fprintf('Line nr %1.f is skipped\n',i)
end
else
fprintf('Line nr %1.f is skipped\n',i)
end
end
end

Best Answer

i=0;
while ~feof(fid)
strLine = fgetl(fid);
numLine = str2num(strLine);
if numel(numLine)>1 && min(numLine(1))>=20 && max(numLine(1))<=200 ...
&& min(numLine(2))>=50 && max(numLine(2))<=500
if issorted(numLine(3:2:end)) && issorted(flip(numLine(4:2:end))) ...
&& length((numLine(4:2:end)))==length((numLine(3:2:end)))
if min(numLine(3:2:end))>=0 && max(numLine(3:2:end))<=360 && ...
min(numLine(4:2:end))>=0 && max(numLine(4:2:end))<=50 && ...
length(numLine(4:2:end))>=2 \
i=i+1;
W(:,i) = numLine(1);
D(:,i) = numLine(2);
t{:,i} = numLine(3:2:end);
C{:,i} = numLine(4:2:end);
else
fprintf('Line nr %1.f is skipped\n',i)
end
else
fprintf('Line nr %1.f is skipped\n',i)
end
else
fprintf('Line nr %1.f is skipped\n',i)
end
end