fgetl skipping empty lines and text lines
Show older comments
So i have some data with unknown number of lines. They contain the numbers 1 or -1, text or empty lines. How do i fgetl to obtain only the numbers(no empty lines and text lines) to store into a matrix? say for example my data is
1 1 -1 1 1 -1
1 -1 1 1 -1 1
sdasr
-1 1 1 1 -1 -1
where --- is an empty line, what should my loop look like using a while loop to extract just the 1s and -1s into the matrix in the correct row and column they come in? I am thinking about using a while loop to fgetl all the lines but don't know what condition to use. Thanks
NB: Edited using {}Code to reflect actual file format -- dpb
2 Comments
dpb
on 6 May 2016
How many columns and are they regular? How large is the file? Unless it's huge it's probably simpler to read into memory and process from there but the specific tests are dependent on the format (other than empty lines are simple, of course).
alexander li
on 6 May 2016
Accepted Answer
More Answers (1)
Image Analyst
on 6 May 2016
Just put
textLine = fgetl(fid);
if isempty(textLine) || length(textLine) == 0
continue; % Skip "empty" lines.
end
% Extract numbers from string.
[whatever, whatever] = sscanf(textLine, '%d,.......
into your loop.
1 Comment
alexander li
on 6 May 2016
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!