I want to extract data from a text file
Show older comments
I am attaching a text file. It contains both numeric and text data. I want to build a matrix out of the numbers from 21st row in the attached file.
I want to make a single row out of the five rows after the 21st row in the attached file. And will like to delete the two rows ( which appear like this: 7.10 157.00 0.00 227.35 17.74 Densities ) which keep on coming after every five useful rows of the data.
Please let me know.
Thanks.
3 Comments
dpb
on 14 Jul 2014
...I am attaching a text file.
Didn't make it...
Image Analyst
on 14 Jul 2014
After you push the "Choose file" button, make sure you click the "Attach File" button.
Dev
on 15 Jul 2014
Accepted Answer
More Answers (2)
dpb
on 15 Jul 2014
1 vote
Actually, there's enough info in the header lines to automate the reading after you get the first header of system time, etc., then the number of lat/long steps are available which determines the number of groups and the number of densities determines the number of values read per group.
This also was clearly a Fortran application-written file (the seemingly spurious "1" and "0" are line printer carriage control characters from days of yore) and it would truly be simpler by far to write a Fortran routine to read it than writing C-style parsing, altho that can be done too, with textscan , fscanf and friends...
Why not give the above hints a go at a start, though? I've got family in so really don't have much time to devote at the moment, sorry...
Image Analyst
on 14 Jul 2014
Edited: Image Analyst
on 15 Jul 2014
0 votes
Try readtable() or importdata().
After looking at the attached file, it's such a unique format with a lot of things that are different line by line. So you're going to have to write your own reader. Use fopen(), fgetl(), strfind(), and fclose(). Read a line, figure out what kind of line it is with strfind(), then extract the numbers and strings, and continue until all lines have been processed.
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!