Skip to content

Instantly share code, notes, and snippets.

@tanitanin
Last active December 24, 2015 06:09
Show Gist options
  • Save tanitanin/6755643 to your computer and use it in GitHub Desktop.
Save tanitanin/6755643 to your computer and use it in GitHub Desktop.
GnuplotをC++で使うためのクラス
#pragma once
#include <iostream>
#include <string>
#include <memory>
#include <cstdio>
#include <cstdarg>
using namespace std;
// 以下のように使う
// GNUPlot gp("gnuplot");
// gp.plot(sin(X));
// Windowsの場合は"gnuplot"を"pgnuplot"に置き換える
class GNUPlot {
public:
GNUPlot(const char *command);
~GNUPlot();
private:
GNUPlot();
public:
void plot(const char *str);
void replot();
void set(const char *str);
void cd(const char *str);
void command(const char *format, ...);
void flush();
void exit();
private:
unique_ptr<FILE> _file_ptr;
};
GNUPlot::GNUPlot(const char *command) : _file_ptr(popen(command,"w")) {
}
GNUPlot::~GNUPlot() {
pclose(_file_ptr.get());
}
void GNUPlot::plot(const char *str) {
command("plot %s", str);
}
void GNUPlot::replot() {
command("replot");
}
void GNUPlot::set(const char *str) {
command("set %s", str);
}
void GNUPlot::cd(const char *str) {
command("cd %s", str);
}
void GNUPlot::flush() {
fflush(_file_ptr.get());
}
void GNUPlot::exit() {
command("exit");
}
void GNUPlot::command(const char *format, ...) {
char buf[1024];
va_list ap;
va_start(ap, format);
vsprintf(buf, format, ap);
va_end(ap);
fprintf(_file_ptr.get(), "%s\n", buf);
flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment