필터 지우기
필터 지우기

Setting a Pivot year when creating Datenum matrix

조회 수: 2 (최근 30일)
Victoria Dutch
Victoria Dutch 2020년 5월 26일
편집: Victoria Dutch 2020년 8월 5일
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.

답변 (1개)

Monisha Nalluru
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
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!
Victoria Dutch
Victoria Dutch 2020년 8월 5일
편집: Victoria Dutch 2020년 8월 5일
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".

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by