Convert from datenum to yymmddHHMMSS

1 visualización (últimos 30 días)
Louise Wilson
Louise Wilson el 29 de Ag. de 2019
Editada: Louise Wilson el 4 de Sept. de 2019
I have a large list of dates which I previously converted from yymmddhhmmss format to datenum format using
dateFormat='yymmddHHMMSS';
date=datenum(date,dateFormat);
Now, at the end of a lot of processing, I'd like to reverse this process, to make presentation of my data more readable.
I have tried this:
formatOut='yymmddHHMMSS'; %convert datenum back to original filename format
short_files=datestr(short_files, formatOut);
out=('short_files.csv'); %.csv filename with serialNo
[~,fnm,ext]=fileparts(out);
out = sprintf('%s_%d%s',fnm,serialNo,ext);
dlmwrite(fullfile(path,out),short_files,'delimiter','');
and while this works in my list of variables, when I export to .csv, the values I get look like this:
1.90704E+11 .

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Ag. de 2019
dlmwrite() assumes that the inputs are numeric unless you specify the 'precision' option with a format code such as '%s'
You should
outfile = fullfile(OutputDirectoryName, out);
fid = fopen(outfile, 'wt');
for K = 1 : size(short_files,1)
fprintf(fid, '%s\n', short_files(K,:));
end
fclose(fid)
Or you should skip the problem by using datetime objects and tables and writetable()
  3 comentarios
Walter Roberson
Walter Roberson el 4 de Sept. de 2019
It appears I was mistaken about dlmwrite() details. What happens if you try
out = datestr(700000+rand(10,1)*1000,'yymmddHHMMSS');
dlmwrite('test.csv', out, 'delimiter', '')
When I try this, I get the expected output.
Note though that this would not be usable to also write any other non-character output at the same time.
Louise Wilson
Louise Wilson el 4 de Sept. de 2019
Editada: Louise Wilson el 4 de Sept. de 2019
Hi Walter, I figured it out by just doing this:
formatOut='yymmddHHMMSS';
short_files=datestr(short_files_list, formatOut);
out=('short_files.csv'); %.csv filename with serialNo
[~,fnm,ext]=fileparts(out);
out = sprintf('%s_%d%s',fnm,serialNo,ext);
dlmwrite(fullfile(folder,out),short_files,'delimiter',''); %write output to separate .csv
...this converts the filename back to the format I am interested in and writes to the csv all good.
Thanks for your help

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by