How do you import specific rows of numeric data (without text) for the file I have attached?
1 view (last 30 days)
Show older comments
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.
Accepted Answer
per isakson
on 6 Mar 2021
Edited: per isakson
on 7 Mar 2021
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
...
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
More Answers (0)
See Also
Categories
Find more on Data Import and Export 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!