# How can I convert NaN values to single -999 value

1 view (last 30 days)
Szabó-Takács Beáta on 20 Jun 2017
Dear Matlab Users,
I use R2015b Matlab. I tried to convert NaN data to -999 and convert the matrix which containes these -999 values to single by:
[ndata,Nest]=size(prC);
data=repmat(NaN,[ndata,nlon,nlat]);
for i=1:nlat
for j=1:nlon
[a1,a2]=min(sqrt((longitude2(i,j)-Struct1.Info.Location(:,1)).^2+(latitude2(i,j)-Struct1.Info.Location(:,2)).^2));
if a1<0.25
data(:,j,i)=prC(:,a2);
end
end
end
data(isnan(data))=single(-999);
pr = permute(data,[2,3,1]);
pr = single(pr);
but when I created netCDF file from these values by:
ncid1=netcdf.create('CM5A-MR_RCA4_dry_power1.nc','NC_NOCLOBBER');
>> londimid=netcdf.defDim(ncid1,'longitude',464);
>> latdimid=netcdf.defDim(ncid1,'latitude',201);
>> timedimid=netcdf.defDim(ncid1,'time',5479);
>> lon_varid=netcdf.defVar(ncid1,'longitude','float',londimid);
>> lat_varid=netcdf.defVar(ncid1,'latitude','float',latdimid);
>> time_varid=netcdf.defVar(ncid1,'time','double',timedimid);
>> pr_varid=netcdf.defVar(ncid1,'pr','float',[londimid latdimid timedimid]);
>> netcdf.endDef(ncid1);
>> netcdf.putVar(ncid1,lon_varid,lon);
>> netcdf.putVar(ncid1,lat_varid,lat);
>> netcdf.putVar(ncid1,time_varid,time);
>> netcdf.putVar(ncid1,pr_varid,pr);
>> netcdf.reDef(ncid1);
>> netcdf.putAtt(ncid1,0,'long_name','longitude');
>> netcdf.putAtt(ncid1,0,'standard_name','lon');
>> netcdf.putAtt(ncid1,0,'units','degrees_east');
>> netcdf.putAtt(ncid1,1,'long_name','latitude');
>> netcdf.putAtt(ncid1,1,'standard_name','lat');
>> netcdf.putAtt(ncid1,1,'units','degrees_north');
>> netcdf.putAtt(ncid1,2,'long_name','time');
>> netcdf.putAtt(ncid1,2,'standard_name','time');
>> netcdf.putAtt(ncid1,2,'units','days since 1949-12-1 00:00:00');
>> netcdf.putAtt(ncid1,3,'_FillValue',-999);
>> netcdf.putAtt(ncid1,3,'long_name','precipitation');
>> netcdf.putAtt(ncid1,3,'standard_name','precipitation');
>> netcdf.putAtt(ncid1,3,'units','mm');
>> netcdf.putAtt(ncid1,3,'cell_methods','time: mean');
netcdf.close(ncid1);
the _FillValue became double even though the pr is single (float). According to my best knowledge the missing values or _FillValues type has to be same as the variables (pr). How can I convert the NaN values to single _FillValue? Thank you for your help in advance! Beata

Walter Roberson on 20 Jun 2017
netcdf.putAtt(ncid1,3,'_FillValue', cast(-999, class(pr)) );
Szabó-Takács Beáta on 20 Jun 2017
Thank you Walter! It works correctly.