readmatrix not doing fixed width delimitation correctly

I have a .txt file, a snippet of which is shown below - hopefully this preserves formattign adequatly, shout if not!
-----------------------------------------------------------------------------
PRES HGHT TEMP DWPT RELH MIXR DRCT SKNT THTA THTE THTV
hPa m C C % g/kg deg knot K K K
-----------------------------------------------------------------------------
1022.0 52 15.8 10.8 72 8.02 295 4 287.2 309.8 288.6
1014.0 118 17.4 10.4 63 7.86 305 10 289.4 311.8 290.8
1002.0 218 17.6 9.6 59 7.54 320 15 290.6 312.2 291.9
1000.0 235 17.6 10.6 64 8.08 320 15 290.8 313.9 292.2
992.0 303 17.1 10.1 63 7.85 325 14 290.9 313.4 292.2
I don't need anything above the first numerical line. When the data is fixed width - and as far as I can see from succesful imports into excel the correct width is 7.
So I do the following
FILENAME = 'sonde.txt';
opts = detectImportOptions('sonde.txt');
opts.DataLines = [5 Inf];
opts.VariableWidths = 7;
sondeData = readmatrix(FILENAME,opts);
I think this should output me a matrix which will be 11 columns wide by n rows deep (in this case 166 but that number can change hence the use of inf below).
What I actually get is a 166 x 2 cell.
So questions in turn:
  • How do I get it import to appear as a matrix not a cell? (do I even need to care about this, can I address items in a cell the same way?)
  • Why is it bringing it in as 2 columns wide?
Thanks!

3 Comments

Attach the file itself, not a replica -- no telling what the pasting into and viewing in the browser may have done.
Unless if is fixed width and has missing values or embedded delimiters, shouldn't need to do anything more exotic than
FILENAME = 'sonde.txt';
sondeData = readmatrix(FILENAME,'numheaderlines',4);
That it does something different when you set the width field and used the import options object implies there's something in the file that doesn't match up with your expectations, but would have to have the file itself to be able to be sure what that might be.
Your suggestion works fine though - so I will go with that. I just overcomplicated it.

Sign in to comment.

Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 13 Jul 2022

Commented:

on 13 Jul 2022

Community Treasure Hunt

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

Start Hunting!