Simple datenum question....I think? I just can't get it to work!

2 vues (au cours des 30 derniers jours)
Red Beard
Red Beard le 7 Nov 2012
Hi all, I'm a bit of a newbie with Matlab so please forgive if this seems a bit basic.
My problem is as follows. I have a string called timestamp which contains a date and a time, e.g. 2009.01.01 00:30. I need to remove the time reading (00:30) and convert the date into a number using the datenum function. I have tried to do this using the following code:
datestamp = identifier.timestamp(1:10) % Removes the time to give only the date
datestamp2 = strrep(datestamp,'.', ',') % Replaces . with , to allow parsing
datestamp3 = datenum(datestamp2) % Should give a number for the date
For some reason this code returns the error,"Error using ==> datevec. 2009 is too large to be a month".
I am obviously doing this the wrong way, but how can I get this to work?
All help is massively appreciated. Thanks.
Red Beard

Réponse acceptée

Red Beard
Red Beard le 7 Nov 2012
Ok, managed to solve my own problem by reordering the datestamp string using the following code:
yyyy = identifier.timestamp(1:4) % Sets yyyy as the year
mmdd = identified.timestamp(6:10) % Sets mmdd as the month and day
mmddyyyy = strcat(mmdd, '.', year) % Concatenates strings 'mmdd' '.' 'year'
mmddyyyy = strrep(mmddyyyy, '.', ',') % Replaces all '.' with ','
datenum(mmddyyyy)
This now gives me a correct number for the date provided in the timestamp. So for 01,01,2009 I get 733774.

Plus de réponses (0)

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by