How to import multiple .nc files of different lengths

1 view (last 30 days)
Hello,
I'm trying to import 164 3-D files of size 192x94x365(6). The files contain gridded climate data and are stored annually.
I'm struggling to import these into a single, large, 4-D file due to the extended length of the leap year files. Matlab returns a dimension mismatch.
I have this:
year=[1851:2014];
for ii=1:164
clear fnpath0 fname0 fname1;
fnpath0 = 'myfilepath';
fname0 = sprintf('uwnd.10m.%d.nc',year(ii));
fname1 = [fnpath0 fname0];
Uwndorg = ncread(fname1,'uwnd');
Uwndall(22:60,30:59,:,ii)=Uwndorg(22:60,30:59,:);
end

Accepted Answer

Eeshan Mitra
Eeshan Mitra on 30 Mar 2017
Depending on the application, and how you want to process the data subsequently, data from matrices of unequal size may be saved using one of the two methods:
  1. Pre-process entries so as to make them all of equal size: For entries corresponding to non- 'leap years', save the 60th entry (for February 29) as NaN (not-a-number). The subsequent entries, starting from the 61st (for March 1) are therefore shifted by 1. The matlab function 'leapyear' can be used to perform checks for every instance in the loop ( https://www.mathworks.com/help/aerotbx/ug/leapyear.html ).
  2. To save data from matrices of unequal size without manipulating entries, cell arrays may be used. Find more information on indexing cell arrays and accessing data in this link: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html

More Answers (0)

Categories

Find more on Climate Science and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!