From 6a8ae2de4261fecd0281685b9eea66bbd82bd7fb Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 1 Aug 2023 08:27:01 +0200 Subject: A4: WIP --- a4/octave.cc | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 a4/octave.cc (limited to 'a4/octave.cc') diff --git a/a4/octave.cc b/a4/octave.cc new file mode 100644 index 0000000..e48fe6c --- /dev/null +++ b/a4/octave.cc @@ -0,0 +1,90 @@ +#include "octave.h" + +const Plot& operator<<(std::ostream& out, const Plot& plot) +{ + out << "x = [ "; + for(const auto& [x,y] : plot.data) + { + out << x << " "; + } + out << "]\n"; + + out << "y = [ "; + for(const auto& [x,y] : plot.data) + { + out << y << " "; + } + out << "]\n"; + out << "scatter(x, y)\n"; + return plot; +} + +const std::string& Plot::getTitle() const +{ + return title; +} + +void Plot::add(const std::pair& point) +{ + data.push_back(point); +} + +void Plot::add(double y) +{ + if(y < 1000.0) + { + add({x, y}); + } +} + +void Plot::setX(double x) +{ + this->x = x; +} + +Octave::Octave(const std::string& file) + : out(file + ".m") + , file(file) +{ +} + +Octave::~Octave() +{ + if(!out.good()) + { + return; + } + + out << "fig = figure()\n"; + out << "hold on;\n"; + + for(const auto& plot : plots) + { + out << plot; + } + + out << "legend ({"; + for(const auto& plot : plots) + { + out << "'" << plot.getTitle() << "' " ; + } + out << "}, 'location', 'east');\n"; + out << "xlabel ('" << x_axis << "');\n"; + out << "ylabel ('" << y_axis << "');\n"; +// out << "set(gca, 'yscale', 'log')\n"; + out << "set(gcf, 'PaperPosition', [0 0 5 5])\n"; + out << "set(gcf, 'PaperSize', [5 5])\n"; + out << "print(fig, '" << file << ".pdf');\n"; +} + +Plot& Octave::add(const std::string& title) +{ + plots.push_back(title); + return plots.back(); +} + +void Octave::setAxis(const std::string& x_axis, const std::string& y_axis) +{ + this->x_axis = x_axis; + this->y_axis = y_axis; +} -- cgit v1.2.3