textread facing unexpected characters. Gives an error.

4 ビュー (過去 30 日間)
Theo
Theo 2015 年 4 月 23 日
コメント済み: Theo 2015 年 4 月 23 日
I'm trying to read the following file using textread with the following command [names, types, x, y, answer] = textread('data.txt', ... '%s %f %s %d %d', 'headerlines', 2)
cg00000292 0.826063401 ATP2A1 16 28890100
cg00002426 0.178659348 SLMAP 3 57743543
cg00003994 0.041451237 MEOX2 7 15725862
cg00005847 0.381918846 HOXD3 2 177029073
cg00006414 NA ZNF425;ZNF398 7 148822837
cg00007981 0.037822681 PANX1 11 93862594
however it gives me the following error
Trouble reading floating point number from file (row 5, field 2) ==> NA ZNF425;ZNF398 7
148822837\n
I understand that textread is expecting a float but reaches the NA and gives the error, now my question is how to tell matlab to replace the unexpected with NaN and proceed instead of breaking the execution.
Help is appreciated :)

採用された回答

per isakson
per isakson 2015 年 4 月 23 日
編集済み: per isakson 2015 年 4 月 23 日
Try
>> cac = cssm
cac =
{6x1 cell} [6x1 double] {6x1 cell} [6x1 int32] [6x1 int32]
where
function cac = cssm()
fid = fopen( 'cssm.txt' );
cac = textscan( fid, '%s%f%s%d%d', 'Delimiter','\t', 'TreatAsEmpty','NA' );
fclose( fid );
end
and cssm.txt contains the sample data of the question
  1 件のコメント
Theo
Theo 2015 年 4 月 23 日
Thanks. It works :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by