Reading numerical data from text file
28 views (last 30 days)
Show older comments
I have a large number of text files containing numerical data of the form:
-1.1389E+00 7.5269E-05 | -1.6667E-01 0.0000E+00
-8.6111E-01 2.1246E-04 | 1.6667E-01 0.0000E+00
-5.8333E-01 8.3611E-04 | 5.0000E-01 0.0000E+00
-3.0556E-01 1.5146E-03 | 8.3333E-01 0.0000E+00
-2.7778E-02 1.9345E-03 | 1.1667E+00 0.0000E+00
2.5000E-01 2.5737E-03 | 1.5000E+00 0.0000E+00
5.2778E-01 4.2668E-03 | 1.8333E+00 0.0000E+00
8.0556E-01 1.3023E-02 | 2.1667E+00 0.0000E+00
1.0833E+00 4.3410E-02 | 2.5000E+00 0.0000E+00
1.3611E+00 8.0197E-02 | 2.8333E+00 0.0000E+00
1.6389E+00 1.6765E-01 | 3.1667E+00 4.0626E-03
1.9167E+00 3.3707E-01 | 3.5000E+00 3.8941E-02
2.1944E+00 1.9501E-01 | 3.8333E+00 3.4817E-01
2.4722E+00 9.7508E-02 | 4.1667E+00 5.0355E-01
2.7500E+00 4.1161E-02 | 4.5000E+00 9.2447E-02
3.0278E+00 8.1076E-03 | 4.8333E+00 8.8021E-03
3.3056E+00 3.3931E-03 | 5.1667E+00 3.4740E-03
3.5833E+00 1.8372E-03 | 5.5000E+00 5.5974E-04
3.8611E+00 2.1554E-04 | 5.8333E+00 0.0000E+00
4.1389E+00 0.0000E+00 | 6.1667E+00 0.0000E+00
------------------------------------------------------
8.2128E+01 | 1.1521E+04
How can I load the numerical columns as a 2-column array (the '|' sign is just a separator).
0 Comments
Accepted Answer
Star Strider
on 28 Jul 2019
I have no idea what you want to do.
You can read the entire file into your MATLAB workspace using load, readmatrix, dlmread, textscan, and probably others. Some of these (e.g. textscan) give you the ability to read some columns and not others.
As a rule, it is likely best to read the entire file, then select or calculate the result of that to get the two columns you want.
5 Comments
Star Strider
on 28 Jul 2019
As always, my pleasure!
The original text file would definitely help if you were still having problems. However since you got it sorted, it is not necessary for you to attach it.
My apologies for overlooking the necessity of using cell2mat with the output of textscan. (I have not used textscan in a while.) So it should have been:
fid = fopen('FileName.txt','rt');
D = textscan(fid,'%f%f%f%f','Delimiter',{'\t','|'}, 'CollectOutput',1);
fclose(fid);
Data = cell2mat(D);
Data1 = Data(:,[1 2]); % Columns 1 & 2
Data2 = Data(:,[3 4]); % Columns 3 & 4
More Answers (0)
See Also
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!