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

3 Ansichten (letzte 30 Tage)
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 Kommentare
Guillaume
Guillaume am 8 Nov. 2019
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)?
RR RR
RR RR am 8 Nov. 2019
Bearbeitet: RR RR am 9 Nov. 2019
I guess the length of columns are 20,12,12,12,12

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
Guillaume am 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.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by