I disagree, it's not that simple. Ok, it depends.
I've chosen to divide the task into two steps
- Read the data-file and put the required data into a containers.Map object. The object may be saved to a mat-file. More data can be added to the object later. There are methods with which one may inspect data interactively.
- Loop over the "keys" of the key-file and print result to the screen. It's a demo after all.
Questions on performance and memory usage are postponed.
Error handling and more remains, e.g. testing and documentation.
 
Demo:
>> specific_data
Key: 19490101T0000, Data:
Key: 19490101T0600, Data:
Key: 19490101T1200, Data:
1.0e+03 *
Columns 1 through 9
0.0010 0 0.0596 0.0436 1.0379 -0.0002 0.0072 0.0093 0.1614
Columns 10 through 13
0.0006 0.0004 0.0560 0.0483
Key: 19490101T1800, Data:
Key: 19490102T0000, Data:
...
where
function specific_data
key_filespec = 'h:\m\cssm\Kos.txt';
met_filespec = 'h:\m\cssm\1948_1950.txt';
lib = containers.Map( 'KeyType', 'char', 'ValueType', 'any' );
lib = met2lib( met_filespec, lib );
fid = fopen( key_filespec );
cac = textscan( fid, '%s' );
fclose(fid);
for kk = 1 : length( cac{1} )
key = datestr( datevec( cac{1}(kk), 'ddmmyyyyHH' ) ...
, 'yyyymmddTHHMM' );
if not(isrow( key ))
keyboard
end
fprintf( '\nKey: %s, Data: \n', key )
if isKey( lib, key )
disp( lib( key ) )
end
pause(0.1)
end
end
and
function lib = met2lib( filespec, lib )
str = fileread( filespec );
cac = strtrim( strsplit( str, 'CENTRES:' ) );
cac(1) = [];
for bb = 1 : length( cac )
block_str = cac{bb};
datetime_str = repmat( '0', 1, 11 );
str = strtrim( block_str(1:12) );
datetime_str( end-length(str)+1 : end ) = str;
timekey = datestr( datevec(datetime_str,'yymmdd HHMM',1940)...
, 'yyyymmddTHHMM' );
colhead_xpr ...
= 'k\s+io\s+lon\s+lat\s+f\s+c\s+dp\s+rd\s+zs\s+up\s+vp\s+lonv\s+latv\s+';
str = regexp( block_str, ['(?<=',colhead_xpr,').+$'], 'match' );
if not( isempty( str ) )
num_val = str2num( str{:} );
else
num_val = [];
end
lib( timekey ) = num_val;
end
end
4 Comments
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/144494-reading-specific-data-from-formatted-txt-files-looks-very-dificult#comment_229034
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/144494-reading-specific-data-from-formatted-txt-files-looks-very-dificult#comment_229034
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/144494-reading-specific-data-from-formatted-txt-files-looks-very-dificult#comment_229037
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/144494-reading-specific-data-from-formatted-txt-files-looks-very-dificult#comment_229037
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/144494-reading-specific-data-from-formatted-txt-files-looks-very-dificult#comment_229039
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/144494-reading-specific-data-from-formatted-txt-files-looks-very-dificult#comment_229039
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/144494-reading-specific-data-from-formatted-txt-files-looks-very-dificult#comment_229040
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/144494-reading-specific-data-from-formatted-txt-files-looks-very-dificult#comment_229040
Sign in to comment.