#ifndef _MATWRITER_HEADER_
#define _MATWRITER_HEADER_
#include <iostream>
#include <string>
#include <vector>
#include <armadillo>
#include </usr/local/MATLAB/R2014b/extern/include/mat.h>


using namespace std;
using namespace arma;

class matWriter{
	string filename;
	MATFile *pFile;
public :
	matWriter(string const& name){
		pFile=matOpen(name.c_str(), "w");
		if (pFile==0){cerr<<"fail to open .mat file";abort();}
	}
	~matWriter(){
		matClose(pFile);
	}
	void write(mat const& var, const char* var_name){
		mxArray* arr =  mxCreateDoubleMatrix(var.n_rows,var.n_cols,mxREAL);		
		memcpy(mxGetPr(arr), var.mem, sizeof(double)*var.n_elem);
		matPutVariable(pFile, var_name, arr);
	}
	void write(double var, string const &var_name){
		mxArray* arr =  mxCreateDoubleMatrix(1,1,mxREAL);		
		memcpy(mxGetPr(arr), &var, sizeof(var));
		matPutVariable(pFile, var_name.c_str(), arr);
	}
	void write(mat const& var, string var_name)
	{
		mxArray* arr =  mxCreateDoubleMatrix(var.n_rows,var.n_cols,mxREAL);		
		memcpy(mxGetPr(arr), var.mem, sizeof(double)*var.n_elem);
		matPutVariable(pFile, var_name.c_str(), arr);
	}
	void write(vec const& var, string const&var_name)
	{
		mxArray* arr =  mxCreateDoubleMatrix(var.n_rows,var.n_cols,mxREAL);		
		memcpy(mxGetPr(arr), var.mem, sizeof(double)*var.n_elem);
		matPutVariable(pFile, var_name.c_str(), arr);
	}
};


#endif//_MATWRITER_HEADER_
