Setting a Pivot year when creating Datenum matrix
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Monisha Nalluru
2020-7-10
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 个评论
Monisha Nalluru
2020-8-5
As you are facing error while saving you can convert the variable into string.
v=datestr(v);
And use something like below to save into netcdf file
ncfilename ='myfiles1.nc';
[a,b] = size(v);
cmode = netcdf.getConstant('NETCDF4');
cmode = bitor(cmode,netcdf.getConstant('CLASSIC_MODEL'));
ncid = netcdf.create(ncfilename,cmode);
IDDim1 = netcdf.defDim(ncid,'charlen',b);
IDDim2 = netcdf.defDim(ncid,'methods',a);
IDVarId = netcdf.defVar(ncid,'v','char',[IDDim1 IDDim2]);
netcdf.endDef(ncid);
netcdf.putVar(ncid,IDVarId,v'); %transpose
netcdf.close(ncid);
In order to check whether the variable is saved properly
new_v = ncread('myfiles1.nc','v');
new_v = new_v' % variable saved into file
Hope this gives an idea!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!