while loop with if-statements to set criteria for data.

2 vues (au cours des 30 derniers jours)
Lars
Lars le 24 Juin 2014
Commenté : dpb le 25 Juin 2014
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

Réponse acceptée

dpb
dpb le 24 Juin 2014
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

Plus de réponses (2)

Lars
Lars le 25 Juin 2014
Hey guys I found the problem. The i variable should be inside the if statement and i needed to make a new variable q for it to work. Thanks anyways. Like this
function [W,D,t,C,filename] = load_data(filename)
fid = fopen(filename,'r');
i=0;
q=0;
while feof(fid) == 0 %
q=q+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
i=i+1;
W(:,i) = numLine(1);
D(:,i) = numLine(2); % (:,i)
t{:,i} = numLine(3:2:end);
C{:,i} = numLine(4:2:end);
else
fprintf('Line nr %1.f is skipped\n',q)
end
else
fprintf('Line nr %1.f is skipped\n',q)
end
else
fprintf('Line nr %1.f is skipped\n',q)
end
end
  3 commentaires
Lars
Lars le 25 Juin 2014
Thanks a lot. Now i see you had already solved the problem. :-)
dpb
dpb le 25 Juin 2014
Altho I'd presumed you didn't really care that much about the skipped line numbers...

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 24 Juin 2014
If you want to make it easy for people to help him, then he should attach the file you're using. I just don't see how it's possible for it to assign column "i" of W, D, t, and c if it prints the "skipped" line. Those columns of the cell arrays t and c should all be null. Other columns may get assigned, but the ones you skipped will be null for t and c.
What is the size of the regular numerical arrays W and D? How can you say W(:,i) for the first time when you have not allocated any rows or columns for W yet?
  2 commentaires
Lars
Lars le 25 Juin 2014
I have now tried to upload the file. Thanks
dpb
dpb le 25 Juin 2014
The issue is that the original increments i every time whether the if conditions are met or not--I moved it to be inside the selection section.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by