Saving and loading Armadillo objects on C++ side

This vignette is adapted from the official Armadillo documentation.

Saving and loading matrices and cubes

Armadillo provides functions to save and load matrices and cubes to and from files. The following file types are supported:

The following file types are supported for fields:

Usage:

.save(filename)
.load(filename)

.save(filename, file_type)
.load(filename, file_type)

.save(stream)
.load(stream)

.save(stream, file_type)
.load(stream, file_type)

.save(hdf5_name(filename, dataset))
.load(hdf5_name(filename, dataset))

.save(hdf5_name(filename, dataset, settings))
.load(hdf5_name(filename, dataset, settings))

.save(csv_name(filename, header))
.load(csv_name(filename, header))

.save(csv_name(filename, header, settings))
.load(csv_name(filename, header, settings))

By providing either hdf5_name(filename, dataset) or hdf5_name(filename, dataset, settings), the file_type type is assumed to be hdf5_binary. The dataset argument specifies an HDF5 dataset name (eg. "my_dataset") that can include a full path (eg. "/group_name/my_dataset"); if a blank dataset name is specified (ie. ""), it is assumed to be "dataset". The settings argument is optional; it is one of the following, or a combination thereof

By providing either csv_name(filename, header) or csv_name(filename, header, settings), the file is assumed to have data in comma separated value (CSV) text format. The header argument specifies the object which stores the separate elements of the header line; it must have the type field<std::string>. The optional settings argument is one of the following, or a combination thereof

Caveats

Examples

Save and load matrices:

[[cpp11::register]] int saveload1_(const int& n) {
  arma::mat A(n, n, fill::randu);

  // default save format is arma_binary
  A.save("A.bin");

  // save in raw_ascii format
  A.save("A.txt", arma::raw_ascii);

  // save in CSV format without a header
  A.save("A.csv", arma::csv_ascii);

  // save in CSV format with a header
  arma::field<std::string> header(A.n_cols);
  header(0) = "foo";
  header(1) = "bar";  // etc
  A.save(arma::csv_name("A.csv", header));

  // save in HDF5 format with internal dataset named as "my_data"
  // see the caveats
  // A.save(arma::hdf5_name("A.h5", "my_data"));

  // automatically detect format type while loading
  arma::mat B;
  B.load("A.bin");

  // force loading in arma_ascii format
  arma::mat C;
  C.load("A.txt", arma::arma_ascii);

  // example of testing for success
  arma::mat D;
  bool ok = D.load("A.bin");

  if(ok == true) {
    message("Matrix loaded successfully");
  } else {
    stop("Problem with loading");
  }

  return 0;
}

Save and load fields:

[[cpp11::register]] int saveload2_(const int& n) {
  arma::field<arma::mat> F(n);

  for (int i = 0; i < n; i++) {
    F(i) = arma::mat(n, n, fill::randu);
  }

  // default save format is arma_binary
  F.save("F.bin");

  // save in PPM format
  F.save("F.ppm", arma::ppm_binary);

  // automatically detect format type while loading
  arma::field<arma::mat> G;
  G.load("F.bin");

  return 0;
}