Concatenate multiple matrices by 3rd dimension using 'for' loop (considering a specific variable in netCDF files)

2 ビュー (過去 30 日間)
Hi. I've got a series of netCDF files (250) each having several variables. I read each file and extracted 'lwe_thickness' variable from each file. It's a 360*180 matrix in each. My objective is to concatenate these matrices by 3rd dimension and get the mean matrix by element for 'lwe_thickness'. I tried the following loop considering just 4 files, but it does not concatenate by 3rd dim. Could anyone please help me find what's going wrong here. (It gives All_LWE = -0.0070 -0.0157 -0.0215 -0.0259 as the ans). (LWE is transposed to agree with [lon] & [lat] dimensions which is for later use)
-Thank you in advance-
Folder = 'C:\Users\Matlab1\ncfiles';
Patternoffiles = fullfile(Folder, '*.nc');
ncfiles = dir(Patternoffiles);
n = length(ncfiles);
for i = 1:n
filename = fullfile(ncfiles(i).folder, ncfiles(i).name);
ncdisp(filename);
LWE=ncread(filename,'lwe_thickness');
LWE=LWE';
All_LWE=cat(3,LWE(1:n))
end

採用された回答

KSSV
KSSV 2022 年 10 月 21 日
編集済み: KSSV 2022 年 10 月 21 日
Folder = 'C:\Users\Matlab1\ncfiles';
Patternoffiles = fullfile(Folder, '*.nc');
ncfiles = dir(Patternoffiles);
n = length(ncfiles);
ALL_LWE = zeros(360,180,n) ; % preallocate/ initialize
for i = 1:n
filename = fullfile(ncfiles(i).folder, ncfiles(i).name);
ncdisp(filename);
LWE=ncread(filename,'lwe_thickness');
LWE=LWE';
All_LWE(:,:,i) = LWE ; % fill the matrix here
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAgriculture についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by