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 Comment

It depends on what you consider as "single array". A cell arry? Then textscan works directly.

Sign in to comment.

 Accepted Answer

Simpler:
format longg
T=readtable('sample.txt'); % your text file , attached sample.txt with this answer
T=table2cell(T);
a=cell2mat(T(cellfun(@isnumeric,T)));
Result=reshape(a,size(T,1),[])
Gives:
Result =
0 0 21.5 0 5896.201 5
0 3 5.2 1 482.107 0
0 0 19.1 0 3148.151 0

More Answers (1)

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

This works really well, but not when there's a space at the start. How could you read that?
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
@Robbie Herring: please upload a sample file by clicking the paperclip button. Descriptions of files are never as good as the files themselves.
I've attached the file. Your method works well if I remove the spaces at the start but not if they're there.
@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.
I've tried removing the ETX characters and that makes no difference. It only works if I remove the opening space from each line. Is there anyway to get around this? Or remove the leading space from each line?
When spaces are present, the code gives:
M =
0×1553 empty double matrix
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.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!