Read numbers and characters from text file into a single array
이전 댓글 표시
I have a .txt file with numbers and characters separated by spaces. The numbers and characters always appear in the same columns. The file looks something like:
0 0 C 21.5 TRC 0 5896.201 5
0 3 B 5.2 BTR 1 482.107 0
0 0 C 19.1 TRC 0 3148.151 0
How would you import the data into a single array? If it is not possible to read both numbers and characters into a single array, then how would you extract just the numbers from the file?
Thank you!
댓글 수: 1
채택된 답변
추가 답변 (1개)
Stephen23
2019년 1월 8일
0 개 추천
opt = {'CollectOutput',true};
fmt = '%f%f%*s%f%*s%f%f%f';
[fid,msg] = fopen('test.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
M = C{1}
Giving:
M =
0.00000 0.00000 21.50000 0.00000 5896.20100 5.00000
0.00000 3.00000 5.20000 1.00000 482.10700 0.00000
0.00000 0.00000 19.10000 0.00000 3148.15100 0.00000
The test file is attached.
댓글 수: 7
Robbie Herring
2019년 1월 8일
Stephen23
2019년 1월 8일
@Robbie Herring: please upload a sample file by clicking the paperclip button. Descriptions of files are never as good as the files themselves.
Robbie Herring
2019년 1월 8일
Stephen23
2019년 1월 8일
@Robbie Herring: I note that every line of that file ends with an "End of Text" character (ETX), which might be causing some problems for the file parser. Try removing those ETX characters.
Robbie Herring
2019년 1월 9일
Robbie Herring
2019년 1월 9일
편집: Robbie Herring
2019년 1월 9일
Stephen23
2019년 1월 9일
As far as I can tell there is no problem with the leading spaces. Most likely you did not specify the format string correctly (the file that you uploaded is totally different to the one you described in your question, so you will need to write an appropriate format string). In future please upload any sample files when you ask the question, because it saves a lot of time. Descriptions are not as good as actual files.
I wrote a small piece of code to automatically generate the format string from the first line of the file, and as far as I can tell your file imported correctly, so I have absolutely no problems with the leading space characters.
opt = {'CollectOutput',true};
[fid,msg] = fopen('sample_text.txt','rt');
assert(fid>=3,msg)
% generate format string:
S = fgetl(fid);
C = regexp(S,'[\w\.+-]+','match');
fmt = repmat({'%f'},1,numel(C));
fmt(isnan(str2double(C))) = {'%*s'};
fmt = [fmt{:}];
frewind(fid)
% import data:
C = textscan(fid,fmt,opt{:});
fclose(fid);
M = C{1};
This imports the following data (the rest of the columns appear to be all zeros)::
>> M(:,[3,22:26,1548:1552])
ans =
16606.90000 -999.00000 -999.00000 -999.00000 -100.00000 -999.00000 2018.00000 8.00000 1.00000 15.00000 6.00000
16551.90000 -999.00000 -999.00000 -999.00000 -100.00000 -999.00000 2018.00000 8.00000 1.00000 15.00000 7.00000
16550.10000 -999.00000 -999.00000 -999.00000 -100.00000 -999.00000 2018.00000 8.00000 1.00000 15.00000 8.00000
16680.20000 -999.00000 -999.00000 -999.00000 -100.00000 -999.00000 2018.00000 8.00000 1.00000 15.00000 9.00000
16479.90000 -999.00000 -999.00000 -999.00000 -100.00000 -999.00000 2018.00000 8.00000 1.00000 15.00000 10.00000
16440.50000 -999.00000 -999.00000 -999.00000 -100.00000 -999.00000 2018.00000 8.00000 1.00000 15.00000 11.00000
The (unchanged) data file is attached.
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!