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

조회 수: 2 (최근 30일)
Lars
Lars 2014년 6월 24일
댓글: dpb 2014년 6월 25일
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
Lars
Lars 2014년 6월 25일
I have now tried to upload the file. Thanks
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