How can I create time in proper format to export to a netCDF (.nc) file in MATLAB?
18 views (last 30 days)
Show older comments
Data
I am trying to create a time dimension using this
'''
t1 = datetime(1901,1,1);
t2 = datetime(2016,12,31);
t = t1:t2;
'''
And create a netCDF file using this
'''
nccreate('prec.nc','Prec',...
'Dimensions',{'time' 42369 'lon' 135 'lat' 129},...
'Format', 'netcdf4');
'''
What I have tried
ncwrite('prec.nc', 'time', t);
Error Message
Error using cast
Unsupported data type for conversion: 'datetime'.
Error in internal.matlab.imagesci.nc/write (line 778)
scale_factor = cast(1, class(varData));
Error in ncwrite (line 87)
ncObj.write(varName, varData, start, stride);
Question
How can I create a daily time dimension that I can write out to a netCDF file? What is the proper date type for this conversion?
0 Comments
Answers (1)
KSSV
on 12 Feb 2020
Convert the dates into numbers using datenum. These numbers can be written into netCDF file. Read about datenum.
3 Comments
KSSV
on 13 Feb 2020
t1 = datetime(1901,1,1);
t2 = datetime(2016,12,31);
t = t1:t2;
t = datenum(t) ;
YOu check the conversion in MATLAB itself before writitng to nc. Convert datetime to datenum and vice versa..once it is confirmed..then write to .nc
See Also
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!