datenum problem in matlab

1 回表示 (過去 30 日間)
Niki-Kuriaki Tsaprazi
Niki-Kuriaki Tsaprazi 2012 年 11 月 7 日
Hi,
I have a problem in a Matlab code. I import dates from excel, then Matlab "reads" them with datestr, but after that, when Matlab convert them to numbers with datenum I have problem. The format of dates are 01/01/2012. When a day of a date is from 13 of a month till the end of the month Matlab read the day as 06/03/0021 or 06/04/0034. From 1st of a motnh till 12 of the month a have no problem because it convert the days to numbers correct..
When I run the code without importing dates from excel, I put them manually on editor I don't have any problem..
Please help me!

採用された回答

Pedro Villena
Pedro Villena 2012 年 11 月 7 日
編集済み: Pedro Villena 2012 年 11 月 7 日
str = '01/01/2012'; %%string of the date, read from XLS file
First, you have to specify the date format that you have in your XLS file ( Control Panel >> Regional Configuration ).
format = 'dd/mm/yyyy';
or
format = 'mm/dd/yyyy';
Then, you could convert the date to number, and the number to date, without mistakes
str = '01/01/2012'; %%string of the date, read from XLS file
n = datenum(str,format);
s = datestr(n,format); %%s must be the equal to str

その他の回答 (2 件)

Titus Edelhofer
Titus Edelhofer 2012 年 11 月 7 日
Hi,
my guess is that your data is of the form dd/mm/yyyy, so first the day, then month, then year. But if you omit the date format when calling datenum, datenum guesses your data to be of American format (month first, so mm/dd/yyyy). That's why the first 12 will work (but give again guessing a wrong number!). To make the long story short, use the specific date format string as Pedro suggested.
Titus

Niki-Kuriaki Tsaprazi
Niki-Kuriaki Tsaprazi 2012 年 11 月 8 日
Pedro and Titus, thank you very much for your answers. You help me a lot!! Now, my matlab file works correctly. Thank you very much!

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by