MATLAB Answers

Datetime in a loop that concatenates NetCDF

8 views (last 30 days)
Tim Holgate
Tim Holgate on 10 Dec 2020
Answered: Eric Sofen on 11 Dec 2020
Hi,
I've written a concatnation loop that is supposed to make an array of NetCDF files. This is the code that I have:
time_all=(Nfiles);
sss_all=zeros(Nfiles, length(lon1), length(lat1));
for i = 1:Nfiles
ncfiles(i).name %this just helps my OCD in listing the name of the file
time1 = ncread(ncfiles(i).name, 'time');
time= datetime(1950,1,1)+days(time1);
sss = ncread(ncfiles(i).name, 'SSS');
%concatdat= cell(lat,lon,sss)
sss_all(Nfiles, :, :) = sss;
time(Nfiles)=time;
end
It concatenates fine; however the 'time= datetime(1950,1,1)+days(time1);' function does not seem to function. It corrects the first files date from days since 01 01 1951 but then fills the rest of the dates with NaT. Any ideas what I am doing wrong?

Answers (1)

Eric Sofen
Eric Sofen on 11 Dec 2020
The last line of your loop:
time(Nfiles)=time;
should be
time_all(Nfiles)=time;
so that you update time_all.
Beyond that, check what time1 is in subsequent loop iterations and make sure that it is numeric so it can be converted to days. I don't see anything wrong with the datetime calculation.

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!