How do you import specific rows of numeric data (without text) for the file I have attached?

5 ビュー (過去 30 日間)
Hi, how can I import the numeric data for each location (there are tables for 6 locations) in the file I have attached? I want to import rows 6-11, 14-45, 58-89 and 91-122 etc (all the arrays separately basically). I would really appreciate if anyone could provide an example code. Thank you in advance.

採用された回答

per isakson
per isakson 2021 年 3 月 6 日
編集済み: per isakson 2021 年 3 月 7 日
Maybe not the most rubust way, but it does the job. (The numerical blocks must be followed by non-numerical text.)
%%
ffs = '1D_spectrum_1.txt';
fid = fopen( ffs, 'r' );
ca0 = textscan( fid, '%f%f', 'HeaderLines', 5, 'CollectOutput',true );
ca1 = textscan( fid, '%f', 'HeaderLines', 2, 'CollectOutput',true );
ca2 = textscan( fid, '%f%f%f', 'HeaderLines',12, 'CollectOutput',true );
ca3 = textscan( fid, '%f%f%f', 'HeaderLines', 1, 'CollectOutput',true );
ca4 = textscan( fid, '%f%f%f', 'HeaderLines', 1, 'CollectOutput',true );
ca5 = textscan( fid, '%f%f%f', 'HeaderLines', 1, 'CollectOutput',true );
ca6 = textscan( fid, '%f%f%f', 'HeaderLines', 1, 'CollectOutput',true );
ca7 = textscan( fid, '%f%f%f', 'HeaderLines', 1, 'CollectOutput',true );
ca8 = textscan( fid, '%f%f%f', 'HeaderLines', 1, 'CollectOutput',true );
[~] = fclose( fid );
and inspect
>> ca0{:}
ans =
-2.3243 50.62
-2.3147 50.609
-2.3041 50.614
...
>> ca1{:}
ans =
0.05
0.0551
0.0607
0.0668
...
>> ca2{:}
ans =
0.0005568 86.6 16.7
0.15 78.1 10.8
1.07 78 10.7
3.711 77.9 10.7
...
And see textscan - Read formatted data from text file or string
In response to the first comment
Caveat: It's risky to, based on one sample file, write functions that reads mixed text files. The format of next file may be slightly different and the function fails. In the current case, may I trust that there is a letter after the single numeric field in line 5?
The documentation of textscan() is large and it takes some time to understand all its features.
Doc says:
  • "[...] The textscan function reapplies formatSpec throughout the entire file and stops [terminates] when it cannot match formatSpec to the data."
  • "[...] If you resume a text scan of a file by calling textscan with the same file identifier (fileID), then textscan automatically resumes reading at the point where it terminated the last read."
  • "[...] textscan skips the header lines, including the remainder of the current line."
Thus the statement
caX = textscan( fid, '%f', 'HeaderLines',4, 'CollectOutput',true );
skips 4 header lines, reads numerical fields and terminates when it encounters an item that '%f' doesn't match. The following statement
ca0 = textscan( fid, '%f%f', 'HeaderLines',1, 'CollectOutput',true );
skips the remainder of the current line, i.e the text "number of locations", reads any number of pairs of numerical fields and terminates when it encounters a non-numerical field.
To "import the number on line 5", replace the statement
ca0 = textscan( fid, '%f%f', 'HeaderLines', 5, 'CollectOutput',true );
by the two statements
caX = textscan( fid, '%f', 'HeaderLines', 4, 'CollectOutput',true );
ca0 = textscan( fid, '%f%f', 'HeaderLines', 1, 'CollectOutput',true );
It works
>> caX{:}
ans =
6
>> ca0{:}
ans =
-2.3243 50.62
-2.3147 50.609
-2.3041 50.614
  2 件のコメント
tng
tng 2021 年 3 月 6 日
Thanks a lot @per isakson, if I want to import the number on line 5, how can I do it? (since there is text after the number)
tng
tng 2021 年 3 月 7 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by