Format of datestr() changes

Hi,
I'm was using datestr() to get the date string instead of number, when I realised that the format of the string changes. I've data over a year with a minute interval.
January-April, June-September and November show the date like this: dd.mm.yyyy HH:MM
while May, October and December show it like this: dd-mmm-yyyy HH:MM:SS
Why is that?

6 Comments

I am not seeing it?
>> datestr(now)
ans =
'12-Oct-2017 03:20:10'
>> datestr(now-30)
ans =
'12-Sep-2017 03:20:26'
>> datestr(now-60)
ans =
'13-Aug-2017 03:20:33'
How did you create the numeric data that you are using with datestr() ?
Well I had multiple excel files with production data from 3 motors over 1 month, with time given as a string. My supervisor wanted to see each motor seperately over a year insted of a month, so I imported the data to matlab, changed it and saved it back to excel with writetable().
I converted the strings to numbers using datenum().
I've multiple years and multiple sets of motors, and they all appear the same way as the example above.
Were the dates definitely given as strings in the excel files? The typical way that date fields are stored in excel is in excel numeric date format.
When you were importing, did you import only partial months at a time? When parsing date strings if there are only partial months, it might guess wrong about whether a field is numeric or numeric day.
Is it possible that the files themselves had different date string formats?
Ummmm.. writetable is typically better at handling output of datetime objects rather than exporting numeric dates, unless it is told which columns are datenum. But perhaps you had strings already in the date fields when you did the writetable() ?
When I read the excel file using [num,txt]=xlsread(), the dates appear in txt, not in num, so as far as I know they're strings.
I import all of them using one script, but they are imported one by one using a loop. The code below is a part of how I read the file.
Everything is fine until I export it back to excel. The dates are not messed up, it's only the format that changes.
for i = 1:12
%Filename and path
FN = sprintf('%d.xls', i);
FNP = fullfile('data','framleidsla',verknavn,ar_st,FN);
if exist(FNP,'file') == 2
%Read file & determine size
[num,txt] = xlsread(FNP);
[~,n] = size(txt);
%Extract time vector
timeFormat = 'dd-mm-yyyy HH:MM';
time = [time ; datenum(txt(3:end,1),timeFormat)];
Have you attempted doing the reading using readtable() and getting datetime objects instead of having to convert yourself ?
As Walter says, readtable is a much better choice for reading dates from an Excel file than xlsread.

Sign in to comment.

Answers (0)

Categories

Commented:

on 13 Oct 2017

Community Treasure Hunt

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

Start Hunting!