Reading Time/Dates from Excel
Afficher commentaires plus anciens
Hey guys,
I have a problem using the xlsread function. In my excel spreadsheet, my dates are given in the following format:
2010-11-9 (November 9th, 2011)
However, when I use the following code:
[T1.Sig, T1.TStr] = xlsread('2011FIELD.xls',3,'A1:B42292');
T1.TNum = datenum(T1.TStr);
T1.TVec = datevec(T1.TNum);
MATLAB reads my dates in the following format:
11/9/2010
Then, when I process the code, I get the following error:
Error in ==> readfield2011 at 10 T2.TNum = datenum(T2.TStr); %Converts date string into numerical format
Caused by: Error using ==> dtstr2dtvecmx Failed on converting date string to date number.
Can someone help me understand what to do at this point? I have tried several things, but I still get the same error.
PS: In Excel, if I have the date in one cell (A1), and the time in another cell (B1), is there any way I can merge the two cells together and combine both information into a single cell? Thanks for all the help.
Réponse acceptée
Plus de réponses (5)
Sean de Wolski
le 3 Jan 2012
How is the date stored in your structure T? When I run:
datenum('11/9/2010')
the conversion works:
ans =
734451
Place a breakpoint on line 10 of readfield2011 and see what T2.Tstr is.
More Copying your data:
A = {
'11/9/2010'
'11/9/2010'
'11/9/2010'
'11/9/2010'
'11/9/2010'
'11/9/2010'
'11/9/2010' };
datenum(A)
%{
ans =
734451
734451
734451
734451
734451
734451
734451}
3 commentaires
Sarah
le 3 Jan 2012
Sean de Wolski
le 3 Jan 2012
Can you provide a small example of the cell array?
Sarah
le 3 Jan 2012
Sarah
le 3 Jan 2012
0 votes
3 commentaires
Walter Roberson
le 3 Jan 2012
Hmmm, the documentation specifically says it can be done, so this must be a datenum bug. This explains why some people were having trouble with some code I had proposed before.
Sarah
le 3 Jan 2012
Walter Roberson
le 3 Jan 2012
That solution shows using cellfun() to process the cell array of date strings.
Sarah
le 3 Jan 2012
6 commentaires
Sarah
le 3 Jan 2012
Sarah
le 3 Jan 2012
Dr. Seis
le 3 Jan 2012
Looks like the values in Sig are just the decimal day (i.e., 12:00:00 PM = 0.5). I wonder why Sig is not a 2 column matrix for you? What does "Raw" look like when you print that out?
Dr. Seis
le 3 Jan 2012
In my xls file, the date info is formatted as "date" and the time info is formatted as "custom". If I convert those cells to text then it coverts the cells from date strings to integers for the date info and from time strings to floating point numbers for time info.
Sarah
le 3 Jan 2012
Dr. Seis
le 3 Jan 2012
What the... so "2010-11-9" converted to "3/10/2011"? I guess I would just convert the format of columns A and B from date/time/custom/whatever to text inside Excel (which should result in numbers like my post above), then read in the data like you did (there should be no string info), then you can sum the columns of Sig (as I do above), then add that value to datenum('30-Dec-1899') so Matlab understands it, then store that value as your T1.TNum. Maybe?
Sarah
le 3 Jan 2012
0 votes
2 commentaires
Dr. Seis
le 3 Jan 2012
I did the datestr just to make sure that the numbers associated with "DateTime + datenum('30-Dec-1899')" are understood by Matlab correctly... it was just a quality check. However, I don't understand why it didn't work for you. Can you post the result of:
1. datestr(datenum('30-Dec-1899')) % Should be '30-Dec-1899'
2. datestr(DateTime)
3. DateTime
Sarah
le 3 Jan 2012
Robert Welsh
le 21 Août 2020
0 votes
Dates are read and reported as days since 1900-01-01, with 1900-01-01 as 1. You can do this by creating a spreadsheet with 1900/01/01, and xlsread will read that as as a numeric 1.
Catégories
En savoir plus sur Spreadsheets dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!