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

10 ビュー (過去 30 日間)
Lynn Montgomery
Lynn Montgomery 2017 年 4 月 12 日
コメント済み: Nalaka H Gamage 2022 年 9 月 23 日
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);

採用された回答

Nirav Sharda
Nirav Sharda 2017 年 4 月 17 日
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.
  3 件のコメント
Pai-Feng Teng
Pai-Feng Teng 2020 年 9 月 9 日
The link has been removed.
Nalaka H Gamage
Nalaka H Gamage 2022 年 9 月 23 日
Could you please share the link again

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNetCDF についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by