Problems in reading large matrix with large empty cells
2 views (last 30 days)
Show older comments
Hello:
I am facing problem in reading a large matrix with header file containing values and missing data/empty cells in each column. A few days ago, I have enquired for similar matrix, which could be read with fixedWidthImportOptions, however with simpler matrix: https://de.mathworks.com/matlabcentral/answers/451154-problems-with-empty-cell-in-a-large-matrix?s_tid=srchtitle
But for this case, matrix dimension are pretty complex with some more values. Could any one pls. suggest how to read in this case, especially for fixing 'VariableWidths'. I am attaching the sample matrix here for your reference.
3 Comments
Accepted Answer
per isakson
on 7 May 2021
Edited: per isakson
on 7 May 2021
This script reads your sample file
%%
opts = fixedWidthImportOptions('NumVariables',36,'DataLines',4,...
'VariableNames',{'INDEX','YEAR','MN','HR','DT','SLP','MSLP','DBT','WBT','DPT','RH','VP','DD','FFF','AW','VV','Cl','A','Cm','A','Ch','A','Dl','Dm','Dh','TC','h','c','a','Ht','R_F','EVP','DW','P','H','WAT'},...
'VariableWidths',[5,5,3,3,3,7,7,6,6,6,4,5,3,4,3,3,2,3,2,3,2,3,2,3,3,3,3,2,2,3,6,5,3,2,2,5],...
'VariableTypes',repmat("double",1,36));
T = readtable('Test_data.txt', opts );
T
Determine the column widths based on the header line (second line). The widths of the columns " Cl A" differ between header and data. The first and last column must be added determined visually.
%%
hdr = 'INDEX YEAR MN HR DT ...SLP ..MSLP ..DBT ..WBT ..DPT .RH ..VP DD FFF AW VV Cl A Cm A Ch A Dl Dm Dh TC h c a Ht ..R/F .EVP DW P H .WAT';
pos = strfind( hdr, ' ' );
dpos = diff(pos)
A ruler like this is useful, especially if the header line is not as good as this one.
%%
chr = '42182 1970 01 03 20 0987.1 1012.5 15.6 15.0 14.6 093 16.6 14 004 12 96 9 5 2 2 0 0 9 9 0 7 5 9 1 30 010.5 01.5';
db_ruler( chr )
0 Comments
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms 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!