summaryrefslogtreecommitdiff
path: root/a4/octave.cc
blob: 2d9a2393f7fa27ef5e96c280e2c9736aee6e1363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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<double, double>& 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;
}