NCEXISTS check if variable exists in NetCDF4 file

Contents

Inputs

Outputs

function exists = ncexists(file, variable)
arguments
  file (1,1) string
  variable (1,1) string
end

exists = false;

try
  ncinfo(file, variable);
  exists = true;
catch e
  if ~strcmp(e.identifier, "MATLAB:imagesci:netcdf:badLocationString") && ...
     ~strcmp(e.identifier, "MATLAB:imagesci:netcdf:unknownLocation") && ...
     ~strcmp(e.message, "NetCDF: Variable not found")

    rethrow(e)
  end
end

end


%!test
%! if !isempty(pkg('list', 'netcdf'))
%! pkg load netcdf
%! fn = tempname();
%! ds = 'a';
%! nccreate(fn, ds)
%! assert(ncexists(fn, ds))
%! assert(!ncexists(fn, 'b'))
%! delete(fn)
%! endif