In an assignment A(I) = B, the number of elements in B and I must be the same. while averaging row in Netcdf file

2 Ansichten (letzte 30 Tage)
Hi,
I want to average the value of CO2 for a fixed height in fixed region in Netcdf file. I wrote code for single file. It's running fine and is given,
alti=ncread('SABER_L2C1.nc','altitude');
co2i=ncread('SABER_L2C1.nc','CO2retr');
timei=ncread('SABER_L2C1.nc','time');
lati=ncread('SABER_L2C1.nc','lat');
loni=ncread('SABER_L2C1.nc','lon');
datei=ncread('SABER_L2C1.nc','date');
for i=1:size(lati,1);
for j=1:size(alti,1);
if lati(i) > -80 && lati(i) <0;
if loni(i) >0 && loni(i) <100;
lat(i)=lati(i);
lon(i)=loni(i);
if alti(j)==120;
alt(j)=alti(j);
co2a(j,i) = co2i(j,i);
co2ab=mean(co2a,2);
date1=mean(datei,1);
end
end
end
end
end
When trying to do it for large number of files (~400) using cell, I am getting error "In an assignment A(I) = B, the number of elements in B and I must be the same". My code using cell is below, nc = cell(1, numfiles);
for k = 1:numfiles
nc= netcdf.open(files(k).name);
alti{k}=ncread(files(k).name,'altitude');
co2i{k}=ncread(files(k).name,'CO2retr');
timei{k}=ncread(files(k).name,'time');
lati{k}=ncread(files(k).name,'lat');
loni{k}=ncread(files(k).name,'lon');
datei{k}=ncread(files(k).name,'date');
end
for k=1:numfiles;
for i=1:size(lati{k},1);
for j=1:size(alti{k},1);
if lati{k}(i) > -80 && lati{k}(i) <0;
if loni{k}(i) >0 && loni{k}(i) <100;
lat{k}(i)=lati{k}(i);
lon{k}(i)=loni{k}(i);
if alti{k}(j)==120;
alt{k}(j)=alti{k}(j);
co2a{k}(j,i) = co2i{k}(j,i);
co2ab(k)=mean(co2a{k},2);
date1(k)=mean(datei{k},1);
end
end
Need help. Thank you so much.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Mai 2017
You have
co2ab(k)=mean(co2a{k},2);
date1(k)=mean(datei{k},1);
if co2a{k} or datei{k} are empty then you would have a problem. If they are vectors then you would be okay provided that co2a{k} is a row vector and datei{k} is a column vector. But if they are 2D arrays, then mean() is going to return a vector of values, and that vector is not going to fit into co2ab(k) or date1(k)
  5 Kommentare

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