Trying to use a for loop to capture data from netCDF output, and then average the data

3 views (last 30 days)
I am trying to grab the "September" and "March" Sea Ice Data from a netCDF output file, and then average the yearly September and March Sea Ice data to produce yearly "minimum" and "maximum" sea ice data. I know the ncread() commands are correct, but my for loop seems to crash (as in MatLab hangs/freezes, with no error message) after calculating all values for A, B, C, D.
What I really am intending to achieve is to have MatLab pull the March sea ice data from year one (data points 18-24), average these, store the result as a new variable, and then move on to the next year (data points 18+(73*1)). And do the same for the September Sea Ice data.
Here is my code thus far:
%read and store UVic Model Variables from NetCDF files
Time = ncread('2000-2500_seaice.nc','time');
SeaIceVol = ncread('2000-2500_seaice.nc','O_icevolN');
SeaIceArea = ncread('2000-2500_seaice.nc','O_iceareaN');
for i=1:501
%Determine which data points fall within "March"
A = 18+(73*(i-1))
B = 24+(73*(i-1))
%Determine which data points fall within "September"
C = 55+(73*(i-1))
D = 61+(73*(i-1))
%Store "March" Only Sea Ice Data
MV(i) = SeaIceVol(A):SeaIceVol(B)
%Take Average of March Values in any given year and store in a new variable
MVbar(i) = mean(MV);
%Store "September" Only Sea Ice Data
SV(i) = SeaIceVol(C):SeaIceVol(D);
%Take Average of September Values in any given year and store in a new variable
SVbar(i) = mean(SV);
MVbar
SVbar
end
Any suggestions?
  3 Comments
Ashish Uthama
Ashish Uthama on 23 Jan 2013
Tyler, could you add in the dimensions of Time, SeaIceVol and SeaIceArea? You could also use the debugger to single step through the code and ensure that your indexing inside the loop is giving you the data you think you need. See link for the debugging process.

Sign in to comment.

Answers (1)

Ashish Uthama
Ashish Uthama on 24 Jan 2013
Edited: Ashish Uthama on 24 Jan 2013
Tyler Herrington:
I solved the issue by changing the SeaIceVol(A):SeaIceVol(B) into a series of commas:
for i=1:500
%Determine which data points fall within "March"
A = 18+(73*(i-1));
B = 24+(73*(i-1));
%Determine which data points fall within "September"
C = 55+(73*(i-1));
D = 61+(73*(i-1));
%Store "March" Only Sea Ice Data
MV(i) = SeaIceVol(A), SeaIceVol(A+1), SeaIceVol(A+2), SeaIceVol(A+3), SeaIceVol(B-2), SeaIceVol(B-1), SeaIceVol(B);
%Take Average of March Values in any given year and store in a new variable
MVbar(i) = mean(MV(i));
%Store "September" Only Sea Ice Data
SV(i) = SeaIceVol(C), SeaIceVol(C+1), SeaIceVol(C+2), SeaIceVol(D+3), SeaIceVol(D-2), SeaIceVol(D-1), SeaIceVol(D);
%Take Average of September Values in any given year and store in a new variable
SVbar(i) = mean(SV(i));
end
  2 Comments
N/A
N/A on 19 Aug 2015
I am currently using the ncread to partially read big (11GB) .nc-files. Matlab starts reading and calculating really fast, but at an unpredictable time the calculation/reading just stop. I monitor this with several windows utilities... hd-usage drops to 0-1% from originally about 90%...
Has anyone got an idea?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!