MATLAB: Convert from datenum to yymmddHHMMSS

MATLAB: Convert from datenum to yymmddHHMMSS

datenumMATLABserial date

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 .

Best Answer

  • 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, '%sn', short_files(K,:));
    end
    fclose(fid)
    Or you should skip the problem by using datetime objects and tables and writetable()
  • Add a Comment

    Your email address will not be published. Required fields are marked *