Matlab creates netcdf file but can't open it later(ERROR: The NetCDF library encountered an error during execution of 'open' function - 'Unknown file format (NC_ENOTNC)'

Hello, I've encountered a problem with NetCDF library, I would be very grateful for any tips how to solve it.
I'm trying to create a netcdffile and write variables in it. But for some reason after creating a file I can't reach it anyhow (nccreate, ncdisp are getting the same error):
ncid = netcdf.create('F:\GLORYS_day\ARC\GLORYS_surf.nc','NETCDF4');
nccreate('F:\GLORYS_day\ARC\GLORYS_surf.nc','Lat','Dimensions',{'Lat' 205});
Error using matlab.internal.imagesci.netcdflib
The NetCDF library encountered an error during execution of 'open' function - 'Unknown file format (NC_ENOTNC)'.
Error in netcdf.open (line 77)
[varargout{:}] = matlab.internal.imagesci.netcdflib('open', ...
Error in internal.matlab.imagesci.nc/openToAppend (line 1250)
this.ncRootid = netcdf.open(this.Filename,'WRITE');
Error in internal.matlab.imagesci.nc (line 126)
this.openToAppend();
Error in nccreate (line 159)
ncObj = internal.matlab.imagesci.nc(ncFile,'a', formatStr);
Error in export_to_netcdf (line 10)
nccreate('F:\GLORYS_day\ARC\GLORYS_surf.nc','Lat','Dimensions',{'Lat' 205});

5 Comments

I have encountered the same problem. I had some scripts that were creating and writing out variables to netCDF files perfectly using Matlab 2022a just yesterday. Today I upgraded to 2023a and they now no longer work, resulting in a similar error traceback as you. Reverting back to 2022a and they work fine again. Not sure what the issue is, but if someone with a similar problem ends up seeing this, I hope that helps some. Maybe it will also bring awareness to the issue.
Hi, I'm teaching Matlab and my students have a variety of versions ranging from 2021a to 2023b and it only worked in my computer, so I'm thinking it has to do with the toolbox library. Ideas?
I see it working in Matlab running on MAc and Linux but not in the windows computers
Same here on MacPro:
Error in grdread2 (line 61)
ncid = netcdf.open(file, 'NC_NOWRITE');
And in:
yrange=netcdf.getVar(ncid,1)';
which seems to call: [varargout{:}] = matlab.internal.imagesci.netcdflib('open', ...
However, the line before is not a problem: xrange=netcdf.getVar(ncid,0)';

Sign in to comment.

Answers (1)

EDIT: My previous response talked about how we can get around the encountered error and get the code working in MATLAB. However, there is a simpler way to accomplish this workflow by using nccreate only. See the following code snippet:
nccreate("GLORYS_surf.nc","Lat",Format="netcdf4",Dimensions={"Lat",205});
We encourage the users to stick to either high-level or low-level netcdf interface and not use them simultaneously.
OLD ANSWER:
I tried the following code in R2022a through R2024a:
>> ncid = netcdf.create('GLORYS_surf.nc','NETCDF4');
>> nccreate('GLORYS_surf.nc','Lat','Dimensions',{'Lat' 205});
and it errors out with the same error message as reported earlier:
Error using matlab.internal.imagesci.netcdflib
The NetCDF library encountered an error during execution of 'open' function - 'Unknown file format
(NC_ENOTNC)'.
The issue is the missing netcdf.close(ncid) after the first statement. Since the file was first created using the low-level NetCDF function netcdf.create, it should be closed before calling nccreate (high-level NetCDF function). The following code works fine:
>> ncid = netcdf.create('GLORYS_surf.nc','NETCDF4');
>> netcdf.close(ncid);
>> nccreate('GLORYS_surf.nc','Lat','Dimensions',{'Lat' 205});
Hope that helps!

Products

Release

R2022b

Tags

Asked:

on 19 Apr 2023

Edited:

on 9 May 2025

Community Treasure Hunt

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

Start Hunting!