how to read a txt file having alphanumeric data and special characters. ? My text file is like this. I tried textread but could not get a result. any suggestion.
古いコメントを表示
1 6:s 6:s 0 0 0 0.11 0 *0.889
2 6:s 6:s 0 0.272 0 0.006 0 *0.722
3 6:s 6:s 0 0.036 0 0 0.001 *0.963
4 6:s 6:s 0 0 0 0 0 *1
5 6:s 6:s 0 0 0 0 0 *1
6 6:s 3:f + 0 0.238 *0.365 0.009 0.033 0.355
7 6:s 5:n + 0 0 0 0 *0.999 0.001
8 6:s 6:s 0 0 0 0 0 *1
3 件のコメント
jonas
2018 年 8 月 14 日
What is your desired output?
Muhammad Shoaib
2018 年 8 月 14 日
Walter Roberson
2018 年 8 月 14 日
How do you want the * to be handled?
採用された回答
その他の回答 (1 件)
str=fileread('notes.txt');
str=regexprep(str,'[:+*a-zA-Z]',' ')
opt = {'Delimiter',' ','MultipleDelimsAsOne',true,'CollectOutput',true};
out = textscan(str,'%f%f%f%f%f%f%f%f%f',opt{:})
7 件のコメント
Note that str2num relies on eval, which makes this a risky method to use with input data, especially with data containing weird random characters.
However note that textscan also works on character vectors (not just files), so you could easily use textscan to efficiently parse the char vector into a numeric array.
jonas
2018 年 8 月 14 日
Oh, didn't know that. Thanks. I like to overuse regexp for everything and figured this could work.
Muhammad Shoaib
2018 年 8 月 14 日
Muhammad Shoaib
2018 年 8 月 14 日
jonas
2018 年 8 月 14 日
No problem! I'll refrain from deleting this answer as it shows how to use regexprep to clean up a textfile but make sure to accept the other one :)
カテゴリ
ヘルプ センター および File Exchange で Language Support についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!