fgetl skipping empty lines and text lines

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 件のコメント

dpb
dpb 2016 年 5 月 6 日
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
alexander li 2016 年 5 月 6 日
The data is unknown but there are 1s and -1s which i'm meant to extract and noise which im meant to ignore such as text characters and empty lines.

サインインしてコメントする。

 採用された回答

dpb
dpb 2016 年 5 月 6 日
編集済み: dpb 2016 年 5 月 6 日

0 投票

No looping needed...as long as the numeric values are regular (ie, same number of columns each record) as shown.
>> c=textread('alex.dat','%s','delimiter','\n','whitespace','') % sample file
c =
'1 1 -1 1 1 -1'
'1 -1 1 1 -1 1'
''
'sdasr'
'-1 1 1 1 -1 -1 '
>> A=str2num(char(c(~cellfun(@(x) all(isletter(x)),c))))
A =
1 1 -1 1 1 -1
1 -1 1 1 -1 1
-1 1 1 1 -1 -1
>>
ADDENDUM
Above will fail if the string happens to have any digits in it; more robust would be two steps; first remove blank lines and then use any instead of all for the alpha test.
isstrprop might turn out useful here, too...
ADDENDUM 2 :)
IF were going to use a loop and fgetl here, I'd do it more like--
A=[]; % allocate accumulator array
while ~feof(fid)
try
A=[A;str2num(fgetl(fid))];
catch
end
end

2 件のコメント

alexander li
alexander li 2016 年 5 月 7 日
Okay so i have the matrix now how do i fprintf it to the command window while keeping each element in the same row and same column? Thanks
dpb
dpb 2016 年 5 月 7 日
disp(A)

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 5 月 6 日

0 投票

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 件のコメント

alexander li
alexander li 2016 年 5 月 6 日
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?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Import and Export についてさらに検索

質問済み:

2016 年 5 月 6 日

コメント済み:

dpb
2016 年 5 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by