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.
조회 수: 7 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2018년 8월 14일
편집: Stephen23
2018년 8월 14일
Here is an efficient solution using textscan. The test file is attached (you did not provide us with a sample file).
opt = {'Delimiter',' +*:','MultipleDelimsAsOne',true,'CollectOutput',true};
fmt = ['%*d%*d%*s%*d%*s',repmat('%f',1,6)];
[fid,msg] = fopen('test.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
Which imports that last six columns of numeric data:
>> C{1}
ans =
0.00000 0.00000 0.00000 0.11000 0.00000 0.88900
0.00000 0.27200 0.00000 0.00600 0.00000 0.72200
0.00000 0.03600 0.00000 0.00000 0.00100 0.96300
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
0.00000 0.23800 0.36500 0.00900 0.03300 0.35500
0.00000 0.00000 0.00000 0.00000 0.99900 0.00100
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
댓글 수: 4
jonas
2018년 8월 14일
Don't forget to accept the answer so that future readers can see that the issue was resolved.
추가 답변 (1개)
jonas
2018년 8월 14일
편집: jonas
2018년 8월 14일
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
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 :)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!