MUQ  0.4.3
SimpleReadWrite.cpp
Go to the documentation of this file.
2 
3 
4 int main()
5 {
6  // Open the HDF5 file for reading and writing
7  auto f = muq::Utilities::OpenFile("Data.h5");
8 
9  // Create a new group. If it already exists, this will do nothing.
10  f.CreateGroup("/NewGroup");
11 
12  // Add an attribute to the group.
13  f["/NewGroup"].attrs["Some Metadata"] = "Created with MUQ!";
14 
15  // Store a vector in the group
16  f["/NewGroup/Ones"] = Eigen::VectorXd::Ones(10);
17 
18  // Groups can also be stored and accessed.
19  auto g = f["/NewGroup"];
20  Eigen::VectorXd ones = g["/Ones"];
21  std::cout << "\nThe content of /NewGroup/Ones is: \n" << ones.transpose() << std::endl;
22 
23  // Add some vector-valued metadata to the new dataset.
24  f["/NewGroup/Ones"].attrs["Meta2"] = Eigen::VectorXd::Random(2).eval();
25 
26  // Store another vector in a different group. The group is automatically generated
27  f["/AnotherGroup/Zeros"] = Eigen::MatrixXd::Zero(5,5);
28 
29  // The content in a dataset can casted to an Eigen::MatrixX using the ".eval()" function.
30  std::cout << "\nThe content of /AnotherGroup/Zeros is:\n" << f["/AnotherGroup/Zeros"].eval() << std::endl;
31 
32  return 0;
33 }
int main()
H5Object OpenFile(std::string const &filename)
Open an HDF5 file and return the root object.
Definition: H5Object.cpp:321