Last active
January 25, 2022 13:56
-
-
Save geraintluff/673e8883bda8e3412c66522883fd1af8 to your computer and use it in GitHub Desktop.
Example of outputting CSV from C++, and picking it up with Python (Matplotlib)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef UTIL_CSV_WRITER | |
#define UTIL_CSV_WRITER | |
/* Optional helper class for outputting CSV data. Use like: | |
CsvWriter csv("out"); // no .csv suffix | |
// write individual values | |
csv.write("foo", "bar"); | |
csv.line(); | |
// or write a whole line at once | |
csv.line("foo", "bar"); | |
*/ | |
#include <fstream> | |
#include <string> | |
#include <sstream> | |
class CsvWriter { | |
std::ofstream csvFile; | |
bool newLine = true; | |
template<typename V> | |
void writeValue(V &&v) { | |
std::stringstream strstr; | |
strstr << v; | |
std::string str = strstr.str(); | |
bool needsQuote = false; | |
for (unsigned i = 0; i < str.size(); ++i) { | |
if (str[i] == ',') { | |
needsQuote = true; | |
break; | |
} | |
} | |
if (needsQuote) { | |
csvFile << "\""; | |
for (unsigned i = 0; i < str.size(); ++i) { | |
if (str[i] == '"') { | |
csvFile << '"'; | |
} | |
csvFile << str[i]; | |
} | |
csvFile << "\""; | |
} else { | |
csvFile << v; | |
} | |
} | |
void writeValue(const double &v) { | |
csvFile << v; | |
} | |
void writeValue(const float &v) { | |
csvFile << v; | |
} | |
public: | |
CsvWriter(std::string name) : csvFile(name + ".csv") {} | |
CsvWriter & write() { | |
return *this; | |
} | |
template<class First, class... Args> | |
CsvWriter & write(const First &v, Args ...args) { | |
if (!newLine) csvFile << ","; | |
newLine = false; | |
writeValue(v); | |
return write(args...); | |
} | |
template<typename... T> | |
CsvWriter & line(T... t) { | |
write(t...); | |
csvFile << "\n"; | |
newLine = true; | |
return *this; | |
} | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include "csv-writer.h" | |
int main() { | |
{ | |
// Write it by hand | |
std::ofstream csvFile("out.csv"); | |
for (float x = 0; x < 1; x += 0.001) { | |
csvFile << x << "," << (x*x) << "\n"; | |
} | |
} | |
{ | |
// Or use the CSV helper class | |
CsvWriter csv("out-helper"); | |
for (float x = 0; x < 1; x += 0.001) { | |
csv.line(x, x*x); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
from matplotlib import pyplot | |
# Load CSV | |
data = numpy.loadtxt("out.csv", delimiter=",") | |
# Basic X/Y plot from the two columns | |
figure, axes = pyplot.subplots() | |
axes.plot(data[:,0], data[:,1]) | |
pyplot.savefig("out.svg") # or PNG, etc. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment