Reading numerical data from text file

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).

 Akzeptierte Antwort

Star Strider
Star Strider am 28 Jul. 2019

1 Stimme

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 Kommentare

Thank you Star Rider! Maybe I can explain what is happening here:
I want the final results to look like this:
-1.1389 7.53E-05
-0.86111 2.12E-04
-0.58333 8.36E-04
-0.30556 0.00151
...
That is, the two left columns (shown in the original question), then the two right columns, without any delimiter. The problem in the case of my files is that when I use:
fid = fopen('FileName.txt','r')
Data = textscan(fid,'%s','delimiter','|')
Then I get a Cell array where the first couple of lines look like this:
{'-1.1389E+00 2.8634E-04 ' }
{'-1.6667E-01 0.0000E+00' }
{'-8.6111E-01 2.5114E-04 ' }
...
And from here on I don't know how to separate the two rows, or how to get a numerical array out of this.
It would definitely help to have your file.
If you want the first two columns as one matrix, and the last two columns as the second matrix, that is straightforward. (I do not know if the ‘|’ is actually in your file, however you can include it in the delimiter cell array without problems.) I assume that your file is also tab-delimited. If it is space-delimited, change the 'Delimiter' cell array to include that.
Try something like this:
fid = fopen('FileName.txt','rt');
Data = textscan(fid,'%f%f%f%f','Delimiter',{'\t','|'}, 'CollectOutput',1);
fclose(fid);
Data1 = Data(:,[1 2]); % Columns 1 & 2
Data2 = Data(:,[3 4]); % Columns 3 & 4
See if that does what you want.
At first, I pasted your suggestion into my program and received the error message:
Index in position 2 exceeds array bounds
(must not exceed 1).
Error in ReadText (line 8)
Data1 = Data(:,[1 2])
% Columns 1 & 2
But then I copied
Data=[Data{:}]
before the line: "Data1=..."
Data=[Data{:}]
Data1 = Data(:,[1 2])
and now it seem to be working perfectly.
Thank you very much!
Saeid
Saeid am 28 Jul. 2019
By the way, if you are still curious, I could send you the original text file. It is the output file of a finite element program and I cannot get it to produce the output in a better format.
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by