How do I read this csv file in and convert the dates?

How can I use readtable() to read in this .csv and convert the dates from AM/PM to 24 hour at the same tieme
NA-KBOS , Hourly Forecast Made Oct 19 2017 1509 UTC
LocalTime, Temp, TempDiff, TempNormal, DewPoint, Cloud Cover, FeelsLikeTemp, FeelsLikeTempDiff, Precip, WindDir, WindSpeed(mph)
10/19/2017 12:00:00 AM,55.04,-2.1,48.75,46.94,0,55.04,-2.1,0,200,8.1,
10/19/2017 1:00:00 AM,53.96,-2.41,47.2,46.94,0,53.96,-2.41,0,180,8.1,
10/19/2017 2:00:00 AM,53.06,-2.66,46,46.94,0,53.06,-2.66,0,210,9.2,

4 Comments

What have you tried and where did you have specific problem?
I tried the following:
B=readtable('filepath\KBOS_20171019.csv','Format','%{MM/DD/YYYY HH:MM:SS PM}D%f%f%f%f%u%f%f%f%u%f')
which doesn't work and I'm not sure why.
I then tried:
B=readtable('filepath\KBOS_20171019.csv')
which read it in ok.
So then i wanted to convert the date and I tried:
B=datestr(B:,1),'MM/DD/YYYY HH:MM:SS');
Which didn't work.
Unfortunately, %D formats can only handle spaces in the case where the whitespace property has been set to exclude space.
Try
temp = datestr( datenum(B(:,1), 'mm/dd/yyyy HH:MM:SS PM'), 'mm/dd/yyyy HH:MM:SS');
B(:,1) = cellstr(temp);
T4H14
T4H14 on 20 Oct 2017
Edited: T4H14 on 20 Oct 2017
When I try that I get:
temp = datestr( datenum(B(:,1), 'mm/dd/yyyy HH:MM:SS PM'), 'mm/dd/yyyy HH:MM:SS');
Error using datenum (line 181) DATENUM failed.
Caused by: Error using datenum (line 109) The input to DATENUM was not an array of character vectors.

Sign in to comment.

 Accepted Answer

Hi, I have two things.
The first is that readtable should recognize the format, and return datetimes (assuming you are in a recent release. I didn't need to pass anything in to get DATETIME out.) If not, then the format should be %{MM/dd/uuuu hh:mm:ss aa}D.
Second, the format you see is only a display format. You can simply change the format of the array to see it in 24 hour time.
This should be as easy as:
T = readtable(filename)
T.LocalTime.Format = 'MM/dd/uuuu HH:mm:ss'
Check out the datetime page for more info on the formats supported. https://www.mathworks.com/help/matlab/ref/datetime.html

More Answers (0)

Categories

Asked:

on 20 Oct 2017

Edited:

on 20 Oct 2017

Community Treasure Hunt

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

Start Hunting!