reading a text file by correct date format
Mostrar comentarios más antiguos
Hi,
I have a text file like below. How can I read the 2nd column/format to read it using MATLAB. I am having a difficulty is reading it using the correct format for yyyy/mm/dd hh:mm. Any help is appreciated.
Thanks in advance.
filePattern = fullfile(myFolder, '*.txt');
csvFiles = dir(filePattern);
fmt='%d %4d/%2d/%2d %4d %*[^\n]';
for i=1:length(csvFiles)
fid = fopen(fullfile(myFolder,csvFiles(i).name));
c=cell2mat(textscan(fid,fmt,'headerlines',18,'collectoutput',1,'delimiter','\t'));
fid=fclose(fid);
end

4 comentarios
dpb
el 31 de Dic. de 2015
Attach a short section of the file so somebody can test, but I'd venture you don't need (or want) the 'delimiter','\t' parameter. When you do that, you get rid of the default other whitespace characters as delimiters and also causes repeated delimiters to be interpreted as multiple; thus returning empty values. Just use the default white space delimiter string until it's shown to be mandatory otherwise.
You can, if you have recent release, also use the '%D' date format to convert directly.
dpb
el 1 de En. de 2016
Again, we can't test your file unless you attach a section of it but as I said earlier, forget setting the 'delimiter' field entirely; let it default. Failing that, again, give us the actual data, not a picture of it.
Damith
el 2 de En. de 2016
Respuesta aceptada
Más respuestas (1)
per isakson
el 2 de En. de 2016
Editada: per isakson
el 2 de En. de 2016
It's a challenge to read files like yours with Matlab.
>> clear
>> [c1,sdn,c3,c4] = cssm( '010802_Q_1997.txt' );
>> whos
Name Size Bytes Class Attributes
c1 20256x1 81024 int32
c3 20256x1 162048 double
c4 20256x1 2286052 cell
sdn 20256x1 162048 double
where
function [c1,sdn,c3,c4] = cssm( filespec )
fmt = '%6c%25c%9c%[^\n]';
fid = fopen( filespec );
cac = textscan( fid, fmt, 'Headerlines',18, 'Whitespace','' );
fclose( fid );
c1 = textscan( cac{1}', '%6d' );
c1 = c1{:};
sdn = datenum( cac{2}, 'yyyy/mm/dd HH:MM' );
str = permute( cac{3}, [2,1] );
ise = arrayfun( @(ix) all(isspace(str(:,ix))), (1:length(str)) );
str( 7:9, ise ) = repmat( permute( 'nan', [2,1] ), 1, sum(ise) );
c3 = textscan( str, '%9f' );
c3 = c3{:};
c4 = strtrim(cac{4});
end
1 comentario
Damith
el 5 de En. de 2016
Categorías
Más información sobre Other Formats en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

