Read Data

netCDF

We recommend using xarray’s open_dataset

For a single file

import xarray as xr
ds = xr.open_dataset(filename)

For multiple files

import xarray as xr
ds = xr.open_mfdataset(list_of_filenames)

R code coming soon!

# Coming soon!

MATLAB provides graphical import tools and two programmatic interfaces for interacting with netCDF files.

The high-level interface is made of eight high-level functions for getting information about netCDF files and their content, creating variables, writing and reading variables and attributes, and adding schema definitions to netCDF files.

filename = "example.nc";
ncdisp(filename);  % Display file content
varname = "peaks";
peaksData = ncread(filename,varname);
surf(peaksData);

The low-level interface is a thin wrapper around the underlying netCDF C library.

filename = "example.nc";
varname = "peaks";
ncID = netcdf.open(filename,"NC_NOWRITE");
varID = netcdf.inqVarID(ncID,varname);
peaksData = netcdf.getVar(ncID,varID);
netcdf.close(ncID);
surf(peaksData);

Notes

  • Unlike MATLAB HDF5 interfaces, netCDF interfaces are not cloud enabled (yet).
  • As HDF5 interfaces support reading netCDF 4 files, users must currently use these interfaces for accessing netCDF 4 files stored on the cloud (see tutorial MATLAB Access Single NASA EarthData L2 NetCDF in this Cookbook).
  • MATLAB netCDF interfaces support OPeNDAP and byte-range for reading data.

HDF5

For HDF5 files, there are two methods we can use: xarray’s open_dataset and h5py

import xarray as xr
ds = xr.open_dataset(filename,
                     group=path_to_H5netCDF_group)
# add in directions for h5py

R code coming soon!

# Coming soon!

MATLAB provides low- and high-level programmatic interfaces for interacting with HDF5 and HDF4 files.

Focusing on HDF5, which is currently more prevalent, the high-level interface is made of seven high-level functions for getting information about HDF5 files and their content, creating datasets, and reading and writing datasets and attributes.

filename = "example.h5";
h5disp(filename);  % Display file content
ds = "/g4/lat";
lat = h5read(filename,ds);

The low-level interface is a thin wrapper around the underlying HDF5 C library. It is made of more than 300 functions organized in packages (H5F for file management, H5D for dataset management, etc.).

filename = "example.h5";
ds = "/g4/lat";
fID = H5F.open(filename); 
dsID = H5D.open(fID,ds);
lat = H5D.read(dsID);
H5D.close(dsID);
H5F.close(fID);

GeoTIFF

For GeoTIFF files, we recommend using rioxarray’s open_rasterio

import rioxarray
xds = rioxarray.open_rasterio("my.tif")

R code coming soon!

# Coming soon!

MATLAB Mapping Toolbox supports interacting with GeoTIFF files through a series of functions for handling raster data (under Raster Data), and the function geotiffread althrough not recommended but for a few edge cases.

[A,R] = readgeoraster("myGeoTiffFile.tif");
mapshow(A,R);

Shapefiles & GeoJSON

To open shapefiles or GeoJSON, we recommend using geopandas’s read_file.

import geopandas as gpd
gdf = gpd.read_file(filename)

R code coming soon!

# Coming soon!

MATLAB Mapping Toolbox supports interacting with shapefiles and GeoJSON files through a series of functions for handling vector data (under Vector Data), in particular readgeotable, shapeinfo, shaperead, and shapewrite.

% Read shapefile of countries specifying the name of the attributes to read.
S = shaperead("Countries.shp","Attributes",["ISO_2DIGIT","CNTRY_NAME"]);

% Read GeoJSON data from a website and save it in the file storms.geojson.
websave("storms.geojson","https://www.spc.noaa.gov/products/outlook/day1otlk_cat.lyr.geojson");
GT = readgeotable("storms.geojson");

CSV

To open CSV files, we recommend using pandas’s read_csv.

import pandas as pd
df = pd.read_csv(filename)

R code coming soon!

# Coming soon!

MATLAB supports interacting with CSV files and other tabular file formats through functions readtable, readtimetable, readcell, and readmatrix, as well as their counterparts for writing to files.

% Read time table and display summary
TT = readtimetable("myTimeTable.csv");
summary(TT)

Advanced workflows can be implemented using detectImportOptions and functions referenced in its documentation.

% Read variables "TaxiIn" and "TaxiOut" from airlines dataset
filename = "airlinesmall.csv";
opts = detectImportOptions(filename);
opts.SelectedVariableNames = ["TaxiIn","TaxiOut"];
T = readtable(filename, opts);

Notes

  • MATLAB provides higher-level mechanisms form importing data files through the function importdata and the Import Tool.
  • A general list of functions and workflows for interacting with deliimited and formatted text files is provided here.

Excel

To open Excel files, we recommend using pandas’s read_excel

import pandas as pd
df = pd.read_excel(filename)

R code coming soon!

# Coming soon!

See section about CSV files.

MAT-Files (.mat)

#coming soon! scipy & .mat

R code coming soon!

# Coming soon!

MATLAB can interact with MAT-Files (.mat) using functions load, save, and through matfile objects.

Notes

Text Data Files

# Coming soon!

R code coming soon!

# Coming soon!

MATLAB provides a series of functions for interacting with text files. In addition to functions listed in sections about CSV and Binary files, MATLAB provides functions fileread for reading the content of a whole file as text, and readlines and writelines for reading/writing lines to text files.

Notes

Binary Files

To open binary files, we recommend using numpy’s from_file. You will need to know the dimensions and data type of the binary file and if there is an offset or header information in the binary file.

import numpy as np
arr = np.from_file(filepath, dtype=data_type).reshape(nrows, ncols)

R code coming soon!

# Coming soon!

MATLAB supports interacting with binary files through low-level IO functions fopen, fclose, ferror, fseek, ftell, feof, fscanf, fprintf, fread, fwrite, and frewind.

Notes

  • MATLAB provides higher-level mechanisms form importing data files through the function importdata and the Import Tool.
  • It is rare that ones must implement custom binary data file readers/writers, because MATLAB and its toolboxes provide functions for reading and writing a large number of data file formats.