G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsCsv.h
Go to the documentation of this file.
1 
15 
16 #include <fstream>
17 
18 #pragma once
19 
20 
21 namespace gismo {
22 
30 template<class T>
31 void gsWriteCsv(std::string const & filename, const gsMatrix<T> & matrix, const std::vector<std::string> & headers = std::vector<std::string>() )
32 {
33  // Define format of .csv file.
34  const static gsEigen::IOFormat CSVFormat(gsEigen::FullPrecision, gsEigen::Aligned, ", ", "\n");
35 
36  std::ofstream csv_file;
37  csv_file.open(filename);
38  GISMO_ASSERT( (headers.empty() || headers.size() == matrix.cols()), "The column headers should be as many as the columns of the matrix provided." );
39 
40  // If column headers are provided, write to file
41  if (! headers.empty())
42  {
43  for ( index_t j=0 ; j < headers.size() ; j++)
44  {
45  csv_file << headers[j];
46  if (headers.size()-1 == j)
47  csv_file << CSVFormat.rowSeparator;
48  else
49  csv_file << CSVFormat.coeffSeparator ;
50  }
51  }
52  // write matrix entries to file
53  csv_file << matrix.format(CSVFormat);
54  csv_file.close();
55 }
56 
57 
58 } // namespace gismo
#define index_t
Definition: gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
void gsWriteCsv(std::string const &filename, const gsMatrix< T > &matrix, const std::vector< std::string > &headers=std::vector< std::string >())
Export a gsMatrix to a .csv (comma separated values) file.
Definition: gsCsv.h:31
Provides forward declarations of types and structs.