Datetime in a loop that concatenates NetCDF

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?

回答 (1 件)

Eric Sofen
Eric Sofen 2020 年 12 月 11 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

リリース

R2020b

タグ

質問済み:

2020 年 12 月 10 日

回答済み:

2020 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by