MATLAB: I want to extract data from a text file

data import from text file

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.

Best Answer

Here is one way
content = fileread( 'PIM3dtry.txt' ) ;
tokens = regexp( content, 'Densities\s+(.*?)\s{4}', 'tokens' ) ;
data = cellfun( @(c) sscanf(c, '%f')', cat(1, tokens{:}), 'Un', 0 ) ;
data = cat( 1, data{:} ) ;
but you'll have to check that it does what you want. An easier way would be to loop over lines with FGETL and use TEXTSCAN or SSCANF on relevant ones.
EDIT: after reading dpb's hints about using the header, here is an update
content = fileread( 'PIM3dtry.txt' ) ;
nAltitudes = str2double( regexp( content, 'altitudes =\s*(\d+)', ...
'tokens', 'once' )) ;
tokens = regexp( content, 'Altitudes\s+(.*?)[\r\n] +[\r\n]', ...
'tokens', 'once' ) ;
altitudes = sscanf( tokens{1}, '%f' )' ;
tokens = regexp( content, 'Densities\s+(.*?)\s{4}', 'tokens' ) ;
data = cellfun( @(c) sscanf(c, '%f')', cat(1, tokens{:}), 'Un', 0 ) ;
data = cat( 1, data{:} ) ;
An alternative for extracting packets of 50 (# altitudes) numbers would be as in e.g.
pattern = sprintf( '(?<=Altitudes\s+)(\S+\s+){%d}', nAltitudes ) ;
matches = regexp( content, pattern, 'match', 'once' ) ;
altitudes = sscanf( matches, '%f' ) ;
Yet, keep in mind that the standard approach would be to loop over the file's lines using FGETL and extract data using TEXTSCAN, SSCAN, etc. There would be a little more logic to implement than with the REGEXP-based approach, but at least you would have a full understanding of every bit of the code.