Making 3d array using 2d arrays and how to do that

I have 438 files which are containing a 2d data. The size of the 2d data varies, sometimes the size is 215x3245 and sometimes this is 215x3246. Now my objective is to make a 3d array od 438x215x3245 or 438x215x3246. I have written the code like below :
files_jan = dir('S5P_OFFL_L2__CH4____2019*.*');
h = zeros(215,3245,438);
for j = 1:438
h(:,:,j) = ncread(files_jan(j).name,'/PRODUCT/methane_mixing_ratio'); % Note that I shifted j to the third position
end
This code is giving error,as i prelocate the data size of 215x3245x438 and sometimes the right side has a dimension of 215x3246x438. How can i solve this problem?

 Akzeptierte Antwort

KSSV
KSSV am 18 Jun. 2021
Bearbeitet: KSSV am 18 Jun. 2021
files_jan = dir('S5P_OFFL_L2__CH4____2019*.*');
h = zeros(215,3245,438);
for j = 1:438
temp = ncread(files_jan(j).name,'/PRODUCT/methane_mixing_ratio'); % Note that I shifted j to the third position
if size(temp,1) ~= 3245
temp = imresize(temp,[3245 438]) ;
end
h(j,:,:) = temp ;
end

5 Kommentare

I got an error like this:
Unable to perform assignment because the size of the left side is 215-by-3245 and the size of the right side is
3245-by-438
KSSV
KSSV am 18 Jun. 2021
Bearbeitet: KSSV am 18 Jun. 2021
Ohh..yes edited the code....
Thanks for your response. But again I got error :
Error using netcdflib
The NetCDF library encountered an error during execution of 'open' function - 'HDF error (NC_EHDFERR)'.
Error in netcdf.open (line 67)
[varargout{:}] = netcdflib ( 'open', filename, varargin{1} );
Error in internal.matlab.imagesci.nc/openToRead (line 1278)
this.ncRootid = netcdf.open(this.Filename,'NOWRITE');
Error in internal.matlab.imagesci.nc (line 121)
this.openToRead();
Error in ncread (line 61)
ncObj = internal.matlab.imagesci.nc(ncFile);
This was stopped at j = 42
KSSV
KSSV am 18 Jun. 2021
This is a different error. Check your ncfile, is the file corrupt or does the file exist or does the file have said variable.
Try reading the file seperately.
okay I will check these things. I want to what did you write in your code. Can you please explain the if loop. I could not understand that part. Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by