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.
%Open the file
ncid = netcdf.create('','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,...)
%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
%Then store the dimension variables in
%Then store my main variable
%We're done, close the netcdf

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!