Reading numerical data from text file
조회 수: 9 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2019년 7월 28일
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
Star Strider
2019년 7월 28일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!