MATLAB Answers

Setting a Pivot year when creating Datenum matrix

14 views (last 30 days)
Victoria Dutch
Victoria Dutch on 26 May 2020
Edited: Victoria Dutch on 5 Aug 2020
Hi,
I need to create a variable that is hourly values for the month of January 2017, measured as number of days since 00:00 01.01.2017.
starttime = datenum(2017, 1, 1, 0, 0, 0);
endtime = datenum(2017, 2, 1, 0, 0, 0);
t = datenum(2017, 1, 1,[0:(endtime-starttime)*24-1].',0,0);
I've written the above to get my fractional day values, but when I add a pivot year (2017) to the end of the line for t, I get this error: "Error using datenum: Too many input arguements". I also get this error if I add 2017 to the end of the starttime and endtime lines.
I know I could just redefine t as [0 : (1/24): 31] in this instance, but as I will have to repeat this for every month in 2017 and 2018, I don't think this is sustainable.

Answers (1)

Monisha Nalluru
Monisha Nalluru on 10 Jul 2020
From my understanding, you are trying to get an array of datetime based on hourly increment of time starting from Jan 2017 to Jan 2018
You can make use below example
starttime=datetime(2017,1,1,0,0,0);
endtime=datetime(2018,1,1,0,0,0);
v=starttime:hours(1):endtime;
datestr(v);
If you want to find the difference between two date vectors/matrices use etime which return duration in seconds and you can convert it into hours based on requirements.
  3 Comments
Victoria Dutch
Victoria Dutch on 5 Aug 2020
Thanks! The times are the axis for all my other variables (I'm looking at hourly weather station measurements), will this still work? I've currently got just time in the square brackets for itself, and time lat and lon in the square brackets for all my measured variables:
starttime=datetime(2017,1,1,0,0,0);
endtime=datetime(2018,1,1,0,0,0);
t=starttime:hours(1):endtime;
t=datestr(t);
% creating file and lat and lon attributes is here
netcdf.reDef(ncid);
varid = netcdf.defVar(ncid,'Time','char',[time]);
nccreate('TVC_Jan2018.nc','Time');
%netcdf.putAtt(ncid,varid,'standard_name','Time');
netcdf.putAtt(ncid,varid,'long_name','Observational time');
netcdf.putAtt(ncid,varid,'units','Number of days since 00:00, 01:01:2017');
ncwriteatt('TVC_Jan2018.nc','Time','Time',t);
netcdf.endDef(ncid);
This code I've written doesn't work though, it produces a variable that is mostly empty but also contains the letters from the names of my other variables at odd intervals.
EDIT: Turns out something else has gone wrong with my code, and every other value in the netcdf is being written as 9.9692e+36, when this is not the value any of them are supposed to have. The time also gets written to this value (except the first 22 values, which are zero) if I put "double" instead of "char".

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!