Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
build
path.hpp
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
project(save_and_load_eigen_csv CXX)
cmake_minimum_required(VERSION 3.0)
find_package(Eigen3 REQUIRED)

include_directories(${CMAKE_SOURCE_DIR})

configure_file(${CMAKE_SOURCE_DIR}/path.hpp.in ${CMAKE_SOURCE_DIR}/path.hpp)

add_executable(double_test double_test.cpp)
target_link_libraries(double_test Eigen3::Eigen)

add_executable(float_test float_test.cpp)
target_link_libraries(float_test Eigen3::Eigen)

add_executable(int_test int_test.cpp)
target_link_libraries(int_test Eigen3::Eigen)
40 changes: 40 additions & 0 deletions double_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "save_load_eigen_csv.hpp"
#include "path.hpp"

int main()
{
// test matrix to be saved
// Eigen::MatrixXd matrix_test(4, 4);
Eigen::Matrix<double, 4, 4> matrix_test;
// define the test matrix
matrix_test << 1.2, 1.4, 1.6, 1.8,
1.5, 1.7, 1.9, 1.10,
0.8, 1.2, -0.1, -0.2,
1.3, 99, 100, 112;

// save test matrix
std::string matrixFile = build_path + std::string("matrix.csv");
saveData<double>(matrixFile, matrix_test);

// matrix to be loaded from a file
Eigen::MatrixXd matrix_test2;

// load the matrix from the file
matrix_test2 = openData<double>(matrixFile);

// print the matrix in console
std::cout << matrix_test2 << std::endl;

// test the load function on a matrix defined outside this environment.
// make sure that the "matrix_outside.csv" file exhists
/*MatrixXd matrix_test3;

matrix_test3 = openData("matrix_outside.csv");

cout <<"\n\n"<< matrix_test3<<"\n\n";

cout << 10*matrix_test3 << "\n";*/
}
40 changes: 40 additions & 0 deletions float_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "save_load_eigen_csv.hpp"
#include "path.hpp"

int main()
{
// test matrix to be saved
Eigen::MatrixXf matrix_test(4, 4);

// define the test matrix
matrix_test << 1.2, 1.4, 1.6, 1.8,
1.5, 1.7, 1.9, 1.10,
0.8, 1.2, -0.1, -0.2,
1.3, 99, 100, 112;

// save test matrix
std::string matrixFile = build_path + std::string("matrix.csv");
saveData<float>(matrixFile, matrix_test);

// matrix to be loaded from a file
Eigen::MatrixXf matrix_test2;

// load the matrix from the file
matrix_test2 = openData<float>(matrixFile);

// print the matrix in console
std::cout << matrix_test2 << std::endl;

// test the load function on a matrix defined outside this environment.
// make sure that the "matrix_outside.csv" file exhists
/*MatrixXd matrix_test3;

matrix_test3 = openData("matrix_outside.csv");

cout <<"\n\n"<< matrix_test3<<"\n\n";

cout << 10*matrix_test3 << "\n";*/
}
40 changes: 40 additions & 0 deletions int_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "save_load_eigen_csv.hpp"
#include "path.hpp"

int main()
{
// test matrix to be saved
Eigen::MatrixXi matrix_test(4, 4);

// define the test matrix
matrix_test << 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16;

// save test matrix
std::string matrixFile = build_path + std::string("matrix.csv");
saveData<int>(matrixFile, matrix_test);

// matrix to be loaded from a file
Eigen::MatrixXi matrix_test2;

// load the matrix from the file
matrix_test2 = openData<int>(matrixFile);

// print the matrix in console
std::cout << matrix_test2 << std::endl;

// test the load function on a matrix defined outside this environment.
// make sure that the "matrix_outside.csv" file exhists
/*MatrixXd matrix_test3;

matrix_test3 = openData("matrix_outside.csv");

cout <<"\n\n"<< matrix_test3<<"\n\n";

cout << 10*matrix_test3 << "\n";*/
}
6 changes: 6 additions & 0 deletions path.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef PATH_HPP
#define PATH_HPP

const auto build_path= "${CMAKE_CURRENT_BINARY_DIR}/";

#endif /* PATH_HPP */
76 changes: 76 additions & 0 deletions save_load_eigen_csv.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef SAVE_LOAD_EIGEN_CSV_HPP
#define SAVE_LOAD_EIGEN_CSV_HPP

#include <iostream>
#include <Eigen/Dense>
#include <fstream>
#include <vector>

template<typename T>
void saveData(std::string fileName, Eigen::Matrix<T,-1,-1> matrix)
{
// https://eigen.tuxfamily.org/dox/structEigen_1_1IOFormat.html
const static Eigen::IOFormat CSVFormat(Eigen::FullPrecision, Eigen::DontAlignCols, ", ", "\n");

std::ofstream file(fileName);
if (file.is_open())
{
file << matrix.format(CSVFormat);
file.close();
}
}


template<typename T>
Eigen::Matrix<T,-1,-1> openData(std::string fileToOpen)
{

// the inspiration for creating this function was drawn from here (I did NOT copy and paste the code)
// https://stackoverflow.com/questions/34247057/how-to-read-csv-file-and-assign-to-eigen-matrix

// the input is the file: "fileToOpen.csv":
// a,b,c
// d,e,f
// This function converts input file data into the Eigen matrix format



// the matrix entries are stored in this variable row-wise. For example if we have the matrix:
// M=[a b c
// d e f]
// the entries are stored as matrixEntries=[a,b,c,d,e,f], that is the variable "matrixEntries" is a row vector
// later on, this vector is mapped into the Eigen matrix format
std::vector<T> matrixEntries;

// in this object we store the data from the matrix
std::ifstream matrixDataFile(fileToOpen);

// this variable is used to store the row of the matrix that contains commas
std::string matrixRowString;

// this variable is used to store the matrix entry;
std::string matrixEntry;

// this variable is used to track the number of rows
int matrixRowNumber = 0;


while (std::getline(matrixDataFile, matrixRowString)) // here we read a row by row of matrixDataFile and store every line into the string variable matrixRowString
{
std::stringstream matrixRowStringStream(matrixRowString); //convert matrixRowString that is a string to a stream variable.

while (std::getline(matrixRowStringStream, matrixEntry, ',')) // here we read pieces of the stream matrixRowStringStream until every comma, and store the resulting character into the matrixEntry
{
matrixEntries.push_back(std::stod(matrixEntry)); //here we convert the string to double and fill in the row vector storing all the matrix entries
}
matrixRowNumber++; //update the column numbers
}

// here we convet the vector variable into the matrix and return the resulting object,
// note that matrixEntries.data() is the pointer to the first memory location at which the entries of the vector matrixEntries are stored;
return Eigen::Map<Eigen::Matrix<T, -1, -1,Eigen::RowMajor>>(matrixEntries.data(), matrixRowNumber, matrixEntries.size() / matrixRowNumber);

}


#endif /* SAVE_LOAD_EIGEN_CSV_HPP */
113 changes: 0 additions & 113 deletions source_file.cpp

This file was deleted.