How to write a large matrix to a nc-file

8 次查看(过去 30 天)
I am trying to write a large matrix to a file using nccreate() and ncwrite(), but MATLAB is unfortunately crushing. Can you please provide a code example of how to write a matrix of, say 40K x 40x, in double precision?

回答(2 个)

Jemima Pulipati
Jemima Pulipati 2020-3-13
Here is a sample code which writes a 400 x 800 matrix to an NC file and retrieves the matrix from the file
and checks whether the data written matches or not using netcdf
numrow = 400;
numcol = 800;
my_data = rand(numrow,numcol);
ncid = netcdf.create('somefile.nc','NC_WRITE');
dimidrow = netcdf.defDim(ncid,'rows',numrow);
dimidcol = netcdf.defDim(ncid,'length',numcol);
varid = netcdf.defVar(ncid,'monthlypdsi','NC_DOUBLE',[dimidrow dimidcol]);
netcdf.endDef(ncid);
netcdf.putVar(ncid,varid,my_data);
netcdf.close(ncid);
ncid2 = netcdf.open('somefile.nc','NC_NOWRITE');
data_copy = netcdf.getVar(ncid2,0);
if isequal(my_data,data_copy)
disp('Data match');
else
disp('Data mis-match');
end
Hope it helps!

Petar Milin
Petar Milin 2020-3-15
编辑:Petar Milin 2020-3-15
Hi Jemima,
I see that your code is very explicit, taking all the steps. Meanwhile, I've tried this and it seems to work. However, I would really appreciate if you can advise whether you do see some (hidden) issues with my "shortened" version:
my_data = rand(400,600);
noX = size(my_data,1);
noY = size(my_data,2);
nccreate("file.nc","MyMatrix","Dimensions", {"x",noX,"y",noY},"FillValue","disable");
ncwrite("file.nc","MyMatrix",my_data);
I have found this on several HowTo's places here, at MATLAB Community/Support.
Please, advise.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by