Error reading .nc4 file: 'Unknown file format (NC_ENOTNC)' (MATLAB Version 2022b)

25 views (last 30 days)
Hello, I've encountered a problem with NetCDF (.nc4) files extraction using MATLAB Version 2022b, I would be very grateful for any tips how to solve it.
dinfo = dir( fullfile('*.nc4'));
num_files = length(dinfo);
filename = fullfile({dinfo.name});
d = filename{1};
ncdisp(d)
ncid = netcdf.open(d, 'NC_WRITE');
[dimname, dimlen] = netcdf.inqVar(ncid,28);
lon = netcdf.getVar(ncid,2);
lat = netcdf.getVar(ncid,3);
[LAT LON] = meshgrid(lat, lon);
LAT = LAT(:);
LON = LON(:);
IMD_cord_all = [LON LAT];
alldata = [];
for i = 1:1 %upto number of files
file_name = filename{i};
ncid = netcdf.open(file_name , 'NC_WRITE');
SM = netcdf.getVar(ncid,28);
sm = [];
for j = 1:size(SM,3);
P = SM(:,:,j);
sm1 = P(:);
sm = [sm,sm1];
end
alldata = [alldata, sm];
end
%%
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', ...
  1 Comment
Walter Roberson
Walter Roberson on 31 May 2023
Those fullfile() waste a bit of time but do not contribute anything in the form they are now. I suggest
ncfiledir = '.'; %adjust to path where the nc4 files are
dinfo = dir( fullfile(ncfiledir, '*.nc4'));
num_files = length(dinfo);
filename = fullfile({dinfo.folder}, {dinfo.name});

Sign in to comment.

Answers (1)

Swaraj
Swaraj on 31 May 2023
Hi Kasi,
I understand that you are getting an error while reading .nc4 file. This error normally occurs when the file is corrupted.
The error message "Error reading .nc4 file: 'Unknown file format (NC_ENOTNC)'" suggests that MATLAB is unable to recognize the file format of the file you are trying to read. This can occur if the file is of an unsupported version or if the file extension is not correctly associated with the file format. Just check once if the file that you are trying to read has the correct extension and file format. If it does not solve the issue, make sure that the file you are trying to read is not corrupted.
Hope it helps!!

Community Treasure Hunt

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

Start Hunting!