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

2 Ansichten (letzte 30 Tage)
Lars
Lars am 24 Jun. 2014
Kommentiert: dpb am 25 Jun. 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

Akzeptierte Antwort

dpb
dpb am 24 Jun. 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

Weitere Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 24 Jun. 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 Kommentare
Lars
Lars am 25 Jun. 2014
I have now tried to upload the file. Thanks
dpb
dpb am 25 Jun. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by