how could i stack multiple satellite data of soil moisture in to one and find out the nanmean of all files? The size of files is 1440*720. The file is in '.nc' format.

1 回表示 (過去 30 日間)
Jincy
Jincy 2023 年 5 月 24 日
編集済み: dpb 2023 年 5 月 24 日
% read the all nc file
file = dir('*.nc');
for i = 1:length(file);
sm(i) = sm(:,:,i);
sm.mean = nanmean(:,:,i)
  2 件のコメント
DGM
DGM 2023 年 5 月 24 日
Do you want the average image?
Or do you want the average value of each image?
Jincy
Jincy 2023 年 5 月 24 日
I want to find out the mean value of 365 day satellite data together

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2023 年 5 月 24 日
編集済み: dpb 2023 年 5 月 24 日
% read the all nc file
d= dir('*.nc'); % dir() returns a struct, not a file or even a file name...
varname='THEVARIABLEYOUWANT'; % set this appropriately
sm=[]; % placeholder to catenate images
for i = 1:numel(d)
ncid = netcdf.open(fullfile(d(i).folder,d(i).name),'NC_NOWRITE');
varid = netcdf.inqVarID(ncid,varname); % find the id number in the file to match
sm=cat(3,sm,netcdf.getVar(ncid,varid)); % read each, add to 3D array
end
sm.mean=mean(sm,3,'omitnan'); % compute mean over images
You may need to add size of array to read; I don't have enough familiarity with netCDF format to know whether the getVar method can return an array by var id or not; would presume it can, but don't know.
You may also be able to use the variable name in the varid location instead of inquiring for its postion; that's not documented in the (pretty poor in comparison to standard MATLAB doc) doc file; the input/output variables sections are notable in their being absent so no descriptions given.
It would be more efficient to preallocate the 3D array and store each into the next plane (or use a cell array of 2D arrays), but unless the number is quite large, the above shouldn't be too bad...

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by