Skip to content

Commit cea1ef4

Browse files
committed
move table writing functions into Table
1 parent 3804b80 commit cea1ef4

File tree

7 files changed

+296
-517
lines changed

7 files changed

+296
-517
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mel_example(tcp)
44
mel_example(udp)
55
mel_example(melnet)
66
mel_example(ctrl_c_handling)
7-
mel_example(datalog)
7+
mel_example(table)
88
mel_example(filter)
99
mel_example(options)
1010
mel_example(pid)

examples/ex_datalog.cpp renamed to examples/ex_table.cpp

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,23 @@ using namespace mel;
2626

2727
int main() {
2828

29-
//=========================================================================
30-
// Basic data logging
31-
//=========================================================================
32-
33-
// instantiate logger
34-
DataLogger logger;
35-
// set headers
36-
logger.set_header({"ColumnA", "ColumnB", "ColumnC", "ColumnD"});
37-
// generate data
38-
for (std::size_t i = 0; i < 100; ++i) {
39-
std::vector<double> row{1.0*i, 2.0*i, 3.0*i, 4.0*i};
40-
logger.buffer(row);
41-
}
42-
// save data to C:\data
43-
logger.save_data("datalogger_data.csv", ".", false);
44-
// wait for file to be saved
45-
sleep(seconds(1));
46-
// read back data with row and column offsets
47-
std::vector<std::vector<double>> data_read;
48-
DataLogger::read_from_csv(data_read, 1, 2, "datalogger_data.csv", ".");
49-
for (auto& row : data_read)
50-
print(row);
51-
52-
//=========================================================================
53-
// Advanced data logging with Tables
54-
//=========================================================================
55-
56-
std::string filename = "table_data.csv";
29+
std::string filename = "table_data.tbl";
5730
Table tab("my_table", { "col 0", "col 1", "col 2" });
5831
tab.push_back_row({ 1.01, 2.02, 3.03 });
5932
tab.push_back_row({ 4.04, 5.05, 6.06 });
6033

6134
std::vector<Table> tabs(3);
6235
tabs[0] = tab;
6336
tabs[1] = tab;
64-
tabs[2].set_col_names({ "col A", "col B" });
37+
tabs[2].rename("my_table2");
38+
tabs[2].set_col_names({ "COL A", "COL B" });
6539
tabs[2].set_values({ {0.03256, -0.23535}, {8, 9}, {-1000, -5000} });
6640

67-
DataLogger::write_to_csv(tabs, filename, ".", false);
41+
Table::write(filename, tabs);
6842

6943
Table new_tab;
7044
std::vector<Table> new_tabs;
71-
if (DataLogger::read_from_csv(new_tabs, filename, ".")) {
45+
if (Table::read(filename, new_tabs)) {
7246
//std::cout << new_tab << std::endl;
7347
for (std::size_t i = 0; i < new_tabs.size(); ++i) {
7448
std::cout << new_tabs[i];

include/MEL/Logging/DataLogger.hpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,6 @@ enum class DataFormat {
4242
//==============================================================================
4343

4444
class DataLogger {
45-
public:
46-
47-
/// Write a vector of strings to a file
48-
static bool write_to_csv(const std::vector<std::string> &header, const std::string &filename, const std::string& directory = ".", bool timestamp = true);
49-
50-
/// Write a vector of vectors to a file
51-
static bool write_to_csv(const std::vector<std::vector<double>> &data, const std::string &filename, const std::string& directory = ".", bool timestamp = true);
52-
53-
/// Write a Table to a file
54-
static bool write_to_csv(const Table &data, const std::string &filename = "", const std::string& directory = ".", bool timestamp = true);
55-
56-
/// Write a vector of Tables to a file
57-
static bool write_to_csv(const std::vector<Table> &data, const std::string &filename = "", const std::string& directory = ".", bool timestamp = true);
58-
59-
/// Read a vector of vectors from a file
60-
static bool read_from_csv(std::vector<std::vector<double>> &data_out, const std::string &filename, const std::string& directory = ".");
61-
62-
/// Read a vector of vectors from a file with a row and column offset
63-
static bool read_from_csv(std::vector<std::vector<double>>& data_out, std::size_t row_offset, std::size_t col_offset, const std::string &filename, const std::string& directory = ".");
64-
65-
/// Read a Table from a file
66-
static bool read_from_csv(Table &data, const std::string &filename, const std::string& directory = ".");
67-
68-
/// Read a vector of Tables from a file
69-
static bool read_from_csv(std::vector<Table> &data, const std::string &filename, const std::string& directory = ".");
70-
71-
static std::string make_csv_header(const Table &table);
72-
73-
static bool parse_csv_header(Table &table, const std::string &header);
74-
75-
7645
public:
7746
/// Constructor.
7847
DataLogger(WriterType writer_type = WriterType::Buffered,

include/MEL/Logging/Table.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,30 @@ class Table {
100100
/// Overload the << stream operator with a Table as the rhs argument
101101
friend std::ostream& operator<<(std::ostream& os, const Table& table);
102102

103+
public:
104+
105+
/// Write a Table to a file
106+
static bool write(const std::string &filepath, const Table &data_in);
107+
108+
/// Write a vector of Tables to a file
109+
static bool write(const std::string &filepath, const std::vector<Table> &data_in);
110+
111+
/// Read a Table from a file
112+
static bool read(const std::string &filepath, Table &data_out);
113+
114+
/// Read a vector of Tables from a file
115+
static bool read(const std::string &filepath, std::vector<Table> &data_out);
116+
103117
private:
104118

105119
bool check_inner_dim(const std::vector<std::vector<double>> &values, std::size_t row_size) const;
106120

121+
private:
122+
123+
static std::string make_header(const Table &table);
124+
125+
static bool parse_header(Table &table, const std::string &header);
126+
107127
private:
108128
std::string name_;
109129

src/MEL/Logging/Csv.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#include <thread>
44

55
namespace mel
6-
{
7-
8-
6+
{
97

108
Csv::Csv() : File() {}
119

0 commit comments

Comments
 (0)