MATLAB Answers

RR RR
0

How to parse a table (in a text file) into matlab cell array where we have some missing values?

Asked by RR RR
on 8 Nov 2019
Latest activity Answered by Guillaume
on 11 Nov 2019
Hello all.
How to parse this table into a cell string (or even a vector for each variable)? Each column represent a variable (we have 5 variables). Please note that we need to ignor "- Psat " also most importantnly some columns have missing values. I would like to just put an empty space (or -999) in those places where we have no value. Also there are some empty spaces at the begining of the first column.
This table is generated by a software and lots of such files are generated. Please note that different tables have the missing values in different columns. However, the number of columns is always 5.
------------------- ------------ ------------ ------------ ------------
2000.000 0.9852 45.7751
1800.000 0.9878 45.6544
1600.000 0.9905 45.5293
1400.000 0.9933 45.3995
1200.000 0.9963 45.2648
1000.000 0.9994 45.1246
961.666 - Psat 1.0000 45.0971 3.6734
800.000 1.1361 0.0553 45.6508 3.0676
600.000 1.4394 0.1258 46.3887 2.3402
400.000 2.1396 0.2041 47.2512 1.6256
200.000 4.6874 0.3085 48.4664 0.8928
------------------- ------------ ------------ ------------ ------------

  2 Comments

That's not a very clever format. In particular, the text in the middle of the numbers is not helping.
It looks like it's fixed width. Does the width of the column changes from file to file? In particular, does the width of the 1st column depend on the length of the text (if present)?
I guess the length of columns are 20,12,12,12,12

Sign in to comment.

Tags

Products

1 Answer

Answer by Guillaume
on 11 Nov 2019

Your file appears to have fixed width fields. the easiest way to import such files is with FixedWidthImportOptions, e.g.:
opts = opts = fixedWidthImportOptions('NumVariables', 5, ...
'VariableNames', {'Pressure', 'something', 'somethingelse', 'andanother', 'xyz'}, ...
'VariableWidths', [21, 13, 13, 13, 13], ...
'VariableTypes', repelem({'double'}, 5), ...
'CommentStyle', '-');
data = readtable('new1.txt', opts)
Note that I included the separating space as part of the field width hence they're 1 higher than you specified.
This works well except for the value that includes '- Psat' since that's no longer convertible to a number. Perhaps the best way to cope with that is to add another variable for that column:
opts = fixedWidthImportOptions('NumVariables', 6, ...
'VariableNames', {'Pressure', 'Comment', 'something', 'somethingelse', 'andanother', 'xyz'}, ...
'VariableWidths', [13, 8, 13, 13, 13, 13], ...
'VariableTypes', [{'double', 'string'}, repelem({'double'}, 4)], ...
'CommentStyle', '-');
data = readtable('new1.txt', opts)
Of course, all of this only works if all your files have the column width.

  0 Comments

Sign in to comment.