필터 지우기
필터 지우기

Read netCDFs in loop and export to table with different sizes

조회 수: 4 (최근 30일)
Siegmund Nuyts
Siegmund Nuyts 2022년 9월 23일
댓글: Siegmund Nuyts 2022년 9월 27일
I am reading several netCDF files with a datetime and corresponding data.
All datafiles have a different lenght, so I thought to find the common range of datetime for all files and save the corresponding data in 1 table so that I have a table like:
[datetime sensor1 sensor2 sensor3 sensor...]
I already found a way to read the files and find the common date but struggle to get evertyhing into 1 table.
Does anyone has a suggestion?
addpath(genpath('./'))
path = 'F:\Waterlevel/';
netCDF = dir([path,strcat('**/Sensor_*')]);
for i = 1:length(netCDF)
baseFilename = netCDF(i).name;
fullFileName = fullfile(path, baseFilename);
fprintf(1, 'Now reading %s\n', fullFileName);
nt = ncread(fullFileName, 'datetime');
Times = datetime(nt, 'convertFrom', 'posixtime', 'Format', 'yyyy-MM-dd HH:mm:ss');
nlevel = ncread(fullFileName, 'level');
[commondates, whereina, whereinb] = intersect(Times,Times(1:length(i)));
firstcommon = commondates(1);
end

채택된 답변

Eric Sofen
Eric Sofen 2022년 9월 23일
편집: Eric Sofen 2022년 9월 26일
I would suggest using timetable and the synchronize method.
I assume nlevel is the sensor data? If so, build up a cell array of timetables in the loop, then synchronize them.
Edit: fixed concatenation code.
addpath(genpath('./'))
path = 'F:\Waterlevel/';
netCDF = dir([path,'**/Sensor_*']);
alldata = {};
for i = 1:length(netCDF)
baseFilename = netCDF(i).name;
fullFileName = fullfile(path, baseFilename);
fprintf(1, 'Now reading %s\n', fullFileName);
nt = ncread(fullFileName, 'datetime');
Times = datetime(nt, 'convertFrom', 'posixtime', 'Format', 'yyyy-MM-dd HH:mm:ss');
nlevel = ncread(fullFileName, 'level');
alldata = [alldata, {timetable(Times,nlevel)}];
end
finaldata = synchronize(alldata{:},'intersection');
  댓글 수: 4
Eric Sofen
Eric Sofen 2022년 9월 26일
Whoops! alldata should be built up as a cell array of tables:
alldata = [alldata, {timetable(Times, nlevel)}];
Siegmund Nuyts
Siegmund Nuyts 2022년 9월 27일
That worked perfectly! Thanks a lot

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by