The NetCDF library encountered an error during execution of 'renameDim' function -
'Operation not allowed in data mode (NC_ENOTINDEFINE)'.
When I use 'nc_drename' function today,I encounter the following error:
Code: Select all
>> nc_drename(fid,'xrho','xi_rho')
Error using matlab.internal.imagesci.netcdflib
The NetCDF library encountered an error during execution of 'renameDim' function -
'Operation not allowed in data mode (NC_ENOTINDEFINE)'.
Error in netcdf.renameDim (line 33)
matlab.internal.imagesci.netcdflib('renameDim', ncid, dimid, new_name);
Error in nc_drename>nc_drename_matlab (line 104)
netcdf.renameDim(ncid, dimid, Dname_new);
Error in nc_drename (line 46)
status = nc_drename_matlab(ncfile, Dname_old, Dname_new, Info);
I was not sure if the problem a coincidence or a memory problem (as some posts in matlab forum have suggested), while I used the function last week and it worked fine at the time......
My matlab version is R2021b. After testing, I find that it is the bug about chosen matlab native interface.This is an example in renameDim.m of matlab native netcdf interface:
Code: Select all
% Example:
% srcFile = fullfile(matlabroot,'toolbox','matlab','demos','example.nc');
% copyfile(srcFile,'myfile.nc');
% fileattrib('myfile.nc','+w');
% ncid = netcdf.open('myfile.nc','WRITE');
% netcdf.reDef(ncid);
% dimid = netcdf.inqDimID(ncid,'x');
% netcdf.renameDim(ncid,dimid,'new_x');
% netcdf.close(ncid);
But in 'nc_drename_matlab' sub-function of nc_drename, command 'netcdf.reDef(ncid)' is missing. So have a try to add it.
Code: Select all
function status = nc_drename_matlab(ncfile, Dname_old, Dname_new, Info)
......
% Open NetCDF file and get variable ID.
ncid = netcdf.open(ncfile, 'nc_write');
netcdf.reDef(ncid); %!!!!!!!!!!!!
dimid = netcdf.inqDimID(ncid, Dname_old);
There is similar bug in nc_attadd or any other netcdf redefine functions. I guess it has something to do with matlab version or chosen netcdf interface.
Have a nice day