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

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

采纳的回答

dpb
dpb 2014-6-24
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

更多回答(2 个)

Lars
Lars 2014-6-25
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 个评论
Lars
Lars 2014-6-25
Thanks a lot. Now i see you had already solved the problem. :-)
dpb
dpb 2014-6-25
Altho I'd presumed you didn't really care that much about the skipped line numbers...

请先登录,再进行评论。


Image Analyst
Image Analyst 2014-6-24
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 个评论
dpb
dpb 2014-6-25
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by