NCEXISTS check if variable exists in NetCDF4 file

Contents

Inputs

Outputs

function exists = ncexists(file, variable)
arguments
  file {mustBeTextScalar}
  variable {mustBeTextScalar}
end

exists = false;

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

    rethrow(e)
  end
end

end