Error using fread.. If i change the yearindex to 1 it can read but then the size of sic becomes 334*448*35, and gives error the number of rows are not equal.
1 view (last 30 days)
Show older comments
clear all;clc;
% latitudes and longitudes and area elements for each grid cell
fid = fopen('psn25lats_v3.dat');
lat = fread(fid,[304,448],'int')./100000;
fclose(fid);
fid = fopen('psn25lons_v3.dat');
lon = fread(fid,[304,448],'int')./100000;
fclose(fid);
fid = fopen('psn25area_v3.dat');
area = fread(fid,[304,448],'int')./1000;
fclose(fid);
%
% path to data directory
%
folderpath = 'C:\Users\SK\Documents\MATLAB\nsidc0051_daily\monthly';
% % months to process
months = [1 2 3 4 5 6 7 8 9 10 11 12]; %1:12;
%
% % years to process
years = 1979:2012;
sic_avg_per_month = zeros(448,304,12);
sic_std_per_month = zeros(448,304,12);
sic_trend_per_month = zeros(448,304,12);
sic_year = zeros(304,448,34);
for mi = 1:12,
yearindex = 0;
% allocate size per month with all years
for yi = years,
if mi<10,
monthpath = strcat('0',num2str(mi));
else
monthpath = num2str(mi);
end
if (yi<=1987 && mi<=08),
filename_str = strcat( 'nt_',num2str(yi),monthpath,'_n07_v1.1_n.bin' );
elseif (yi>=1987 && mi>08) && (yi<1992),
filename_str = strcat( 'nt_',num2str(yi),monthpath,'_f08_v1.1_n.bin' );
elseif (yi>1991) && (yi<=1995 && mi<10),
filename_str = strcat( 'nt_',num2str(yi),monthpath,'_f11_v1.1_n.bin' );
elseif (yi<=1995 && mi>=10),
filename_str = strcat( 'nt_',num2str(yi),monthpath,'_f17_v1.1_n.bin' );
end
fid = fopen(filename_str,'r');
sic = fread(fid,[304 448],'int8');
fclose(fid);
sic(sic<0)=NaN; % remove land and pole
yearindex = yearindex+1;
sic_year(:,:,yearindex) = sic;
end
sic_avg_per_month(:,:,mi) = rot90(mean(sic_year,3) ); % average
sic_std_per_month(:,:,mi) = rot90(std(sic_year,0,3) ); % standard deviation
for i=1:448,
for j=1:304,
if (sum(sic_year(j,i,:))== 0 || sum(isnan(sic_year(j,i,:)))>0), % has NaN
sic_trend_per_month(abs(i-449),j,mi) = NaN;
else
stats = regstats(squeeze(sic_year(j,i,:)), [1979:2012]','linear',{'beta' 'rsquare' 'fstat'});
sic_trend_per_month(abs(i-449),j,mi) = stats.beta(2);
end
end
end
end
1 Comment
Walter Roberson
on 8 Mar 2016
What is the error message you are getting with fread() ?
Are you sure it is "sic" that is becoming 334*448*35 and not "sic_year" ?
Answers (0)
See Also
Categories
Find more on Model Building and Assessment 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!