years read in as 18 instead of 2018; how do I add 2000 years?

8 views (last 30 days)
I checked similar questions asked and it seems like the previous answers aren't quite what I am looking for. I have a csv file with a column of date and time. The dates are all mm/dd/18 not mm/dd/2018 so the years read into matlab as 18 not 2018. How can I add 2000 to the year in my timestamps (after importing to matlab). I tried using the addtodate function to add 2000 years (see below) but I got an error that says "Date number must be a numeric scalar." I'm not sure which value is my date number. I am sure there are several ways to do this. I am just looking for a straightforward way to save my year data correctly so that the matlab timestamps are correct. Thank you!
I have also attached the csv file here.
mba = readtable('2018_irradiance.csv');
irrad = mba.PAR;
timestamp = datenum(mba.PST);
times = addtodate(timestamp, 2000, 'year');

Accepted Answer

Adam Danz
Adam Danz on 27 Sep 2019
Edited: Adam Danz on 2 Oct 2019
Read the data in as text rather than datetime by using the DateTimeType parameter of readtable(). Then specify the datetime format in datenum().
mba = readtable('2018_irradiance.csv','DateTimeType','text');
irrad = mba.PAR;
timestamp = datenum(mba.PST,'mm/dd/yy HH:MM');
However, I recommend that you do not split up the variables stored in the mba table. Tables are a great way to keep data organized. To change the date format in the table, use datetime with a specified input format (*note that it's a different format than datenum).
mba = readtable('2018_irradiance.csv','DateTimeType','text');
mba.PST = datetime(mba.PST,'InputFormat','MM/dd/yy HH:mm');
% display first few rows
head(mba)
ans =
8×2 table
PST PAR
____________________ ____
03-Jan-2018 12:28:00 87.2
03-Jan-2018 12:30:00 81
03-Jan-2018 12:32:00 75.3
03-Jan-2018 12:34:00 77.9
03-Jan-2018 12:36:00 83.2
03-Jan-2018 12:38:00 82.1
03-Jan-2018 12:40:00 82
03-Jan-2018 12:42:00 77.2

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!