Read numbers and characters from text file into a single array
Show older comments
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 Comment
Accepted Answer
More Answers (1)
Stephen23
on 8 Jan 2019
0 votes
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 Comments
Robbie Herring
on 8 Jan 2019
Stephen23
on 8 Jan 2019
@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
on 8 Jan 2019
Stephen23
on 8 Jan 2019
@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
on 9 Jan 2019
Robbie Herring
on 9 Jan 2019
Edited: Robbie Herring
on 9 Jan 2019
Stephen23
on 9 Jan 2019
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.
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!