How to write to csv with full display of date format

Hi, I have the following data,and I want to write to csv as it is (i mean date also with full display).
a={'name1' '2016/3/18 20:24:45.678' '23'}
I use the following command,
xlswrite('outputData.csv',a)
But I lose the date format (display), I am getting the following output in csv file:
name1 24:45.7 23
How to retain the date dispaly (2016/3/18 20:24:45.678, not as 24:45.7). Please help some one.
Sincerely, Mekala

Answers (1)

It works as expected under Matlab 2015b and Office 2010, so I guess your file is okay, but you display it in Excel, which has the power to show dates with smart filters.
But note, that the created file is not a CSV file, but an Excel file. If you want a CSV file, using csvwrite or dlmwrite will fail also, because they do not treat the cell string correctly.
Therefore a hard coding might be useful:
fid = fopen(FileName, 'w');
if fid == -1, error('Cannot open file for writing: %s', FileName); end
fprintf('%s,%s,%s\n', a{:});
fclose(fid);

4 Comments

Sir, where to export or define the input? I use your code as below,
a={'name1' '2016/3/18 20:24:45.678' '23'};
FileName=a;
fid = fopen(FileName, 'w');
if fid == -1, error('Cannot open file for writing: %s', FileName); end
fprintf('%s,%s,%s\n', a{:});
fclose(fid);
It gives mefollowing error,
??? Error using ==> fopen Invalid filename.
Error in ==> datawrite2CSV at 16 fid = fopen(FileName, 'w');
"a" is the data, not the name of the file. Use this instead:
FileName = fullfile(cd, 'outputData.csv')
Or avoid cd and the bad habits that it allows, by using pwd:
fullfile(pwd,...)
Sir,
How can I write to ".xlsx" file, Because that is my main issue, I want to write to ".xlsx" and retain the date dispaly. Sincerely,

Sign in to comment.

Asked:

on 19 Mar 2016

Edited:

on 19 Mar 2016

Community Treasure Hunt

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

Start Hunting!