MATLAB Answers

How to create a netCDF file with a Multidimensional Variable in MATLAB?

43 views (last 30 days)
Hi all,
I'm trying to create a netcdf file with three dimensions (time, lat, and lon) and one variable, density which is a function of these. I can put the dimensions in the file successfully but when I try to put the density in, I get some major issues.
My dimensions are 1 column and 830,788 rows. I want my density to be a 4 column (time, lat, lon, density) by 830,788 row array.
I know I'm getting my errors when I try to put density into a variable (second to last line) because of issues with the start and count.
When I try to put in the values of [0 0 0] for the start and [830788 830788 830788] for the count it says that the array is only 830788x1. How can I change this to make it work?
Any tips or help would be great.
Thanks.
%Open the file
ncid = netcdf.create('XXXX.nc','NC_WRITE');
%Define the dimensions
dimidt = netcdf.defDim(ncid,'time',length(date));
dimidlat = netcdf.defDim(ncid,'latitude',length(lat));
dimidlon = netcdf.defDim(ncid,'longitude',length(lon));
%Define IDs for the dimension variables (pressure,time,latitude,...)
date_ID=netcdf.defVar(ncid,'time','double',[dimidt]);
latitude_ID=netcdf.defVar(ncid,'latitude','double',[dimidlat]);
longitude_ID=netcdf.defVar(ncid,'longitude','double',[dimidlon]);
%Define the main variable ()
density_ID = netcdf.defVar(ncid,'density','double',[dimidt dimidlat dimidlon]);
%density_ID = netcdf.defVar(ncid,'density','double',netcdf.getConstant('NC_UNLIMITED'));
%We are done defining the NetCdf
netcdf.endDef(ncid);
%Then store the dimension variables in
netcdf.putVar(ncid,date_ID,0,830788,date);
netcdf.putVar(ncid,latitude_ID,0,830788,lat);
netcdf.putVar(ncid,longitude_ID,0,830788,lon);
%Then store my main variable
netcdf.putVar(ncid,density_ID,density);
%We're done, close the netcdf
netcdf.close(ncid);

Accepted Answer

Nirav Sharda
Nirav Sharda on 17 Apr 2017
Here is a MATLAB Answers post that talks about a matrix can be written to a NC file. Here is the link. I hope this helps.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!