combining multiple .asc files to generate multidimensional raster

I have 5 of .asc files each of dimensions 18*34. I need to combine these files to generate a multidimensional matrix with dimensions 18*34*5.
I am using the following code; but the dimensions of output matrix are 1*1*5.
Where am I wrong?
path = 'E:\SPI';
Pattern = fullfile(path, 'CDR*');
Files = dir(Pattern);
A = [];
for k = 1 : length(Files)
baseFileName = Files(k).name;
fullFileName = fullfile(path, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data = importdata(fullFileName);
A = cat(3, A, data);
save('A.mat','A');
end
size(A)

Answers (1)

Try this
path = 'E:\SPI';
Pattern = fullfile(path, 'CDR*');
Files = dir(Pattern);
A = cell(numel(Files));
for k = 1 : numel(Files)
baseFileName = Files(k).name;
fullFileName = fullfile(path, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data = readmatrix(fullFileName);
A{k} = data;
end
A = cat(3, A{:})
save('A.mat','A');

Categories

Find more on Weather and Atmospheric Science in Help Center and File Exchange

Asked:

on 18 Oct 2020

Commented:

on 18 Oct 2020

Community Treasure Hunt

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

Start Hunting!