How do I keep MATLAB from truncating my date numbers?
Mostra commenti meno recenti
I have data (a lot of data: hundreds of thousand of observations) recorded at one minute intervals over several months in an Excel spreadsheet. The code I use to get the time is:
[NUM,TXT,RAW] = xlsread(filename)
time_string = TXT(:,1);
time = datenum(time_str);
The problem: the time strings in TXT are read correctly (Example: '1/1/2014 12:02:00 AM'), but in the step where they are converted to numbers, MATLAB doesn't keep anything to the right of the decimal.
For example, '1/1/2014 12:02:00 AM' should be "735600.0013888" (found from converting a single date from the time string above - it works for that), but when converting the whole list "time_string", MATLAB truncates this and all the other dates to 735600, which converts to 01-Jan-2014.
Why does it do that? How can I make MATLAB keep the whole number?
1 Commento
the cyclist
il 3 Giu 2014
Are you able to give a small, self-contained example that we can run, that exhibits the problem?
Risposta accettata
Più risposte (4)
the cyclist
il 3 Giu 2014
I think this is a longshot, but does it help if you add the second argument, specifying the date string format?
datenum('1/1/2014 12:02:00 AM','mm/dd/yyyy HH:MM:SS')
This should also have the benefit of speeding up that line a lot. (At least, that is my experience.)
Andrei Bobrov
il 3 Giu 2014
time = datenum(time_string, 'mm/dd/yyyy HH:MM:SS AM');
Mike
il 4 Giu 2014
Mike
il 6 Giu 2014
Categorie
Scopri di più su Dates and Time in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!