劳动节闲来无事,写了一天程序,just for fun.
看,这是C++调用Matlab画图的一段程序。暂时不想多解释了,有兴趣的话,看看下面的代码吧。
以下几段代码由上到下,越来越旧。最上面的是最新更新的版本。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
#include <Eigen/Eigen>
#include <engine.h>
#include <boost/algorithm/string.hpp>
class MatArray
{
public:
MatArray() : _data(NULL){}
MatArray(size_t irows, size_t icols){
resize(irows, icols);
}
MatArray(const MatArray &obj){
if (obj._data){
_data = mxCreateDoubleMatrix(obj.rows(), obj.cols(), mxREAL);
memcpy(this->ptr(), obj.ptr(), sizeof(double)*rows()*cols());
}
else{
_data = NULL;
}
}
~MatArray(){ mxDestroyArray(_data); _data = NULL; }
inline size_t rows() const { return _data ? mxGetM(_data) : 0; }
inline size_t cols() const { return _data ? mxGetN(_data) : 0; }
inline double* ptr() const { return _data ? mxGetPr(_data) : NULL; }
bool resize(size_t irows, size_t icols){
if (!_data){
_data = mxCreateDoubleMatrix(irows, icols, mxREAL);
return (_data != NULL);
}
if (rows() == irows || cols() == icols){
return true;
}
mxDestroyArray(_data);
_data = mxCreateDoubleMatrix(irows, icols, mxREAL);
return (_data != NULL);
}
int put(Engine *ep, const char* var_name){
return engPutVariable(ep, var_name, _data);
}
template<class EigenMat = Eigen::MatrixXf>
void copy_from_eigen(const EigenMat &emat){
if (emat.rows()*emat.cols() == 0){
mxDestroyArray(_data); _data = NULL;
}
resize(emat.rows(), emat.cols());
for (int c = 0; c < emat.cols(); c++){
for (int r = 0; r < emat.rows(); r++){
(*this)[r + c*(int)(emat.rows())] = emat(r, c);
}
}
}
inline double& operator[](int i){
return ptr()[i];
}
private:
mxArray *_data;
};
string rndcolor(){
string color = "[";
color += to_string((rand() % 256) / 255.) + ",";
color += to_string((rand() % 256) / 255.) + ",";
color += to_string((rand() % 256) / 255.) + "]";
return color;
}
class Matlab
{
private:
Matlab(const Matlab &obj){}
public:
Matlab(){
_engine = engOpen(NULL);
if (!_engine){
cerr << "failed to open MATLAB engine!" << endl;
}
else{
cout << "MATLAB has been started successfully!" << endl;
}
}
~Matlab(){
// if you are testing algorithm, you are encouraged to keep the line below bing committed.
//engClose(_engine); _engine = NULL;
}
// line_spec : "LineStyle" + "Marker" + "Color", e.g. "-or"
// for line
// "LineStyle" = {"none", "-", ":", "-."}
// "LineWidth" = 0.5
// "Color" = {[0 0.4470 0.7410] (default) | RGB triplet | {y,m,c,r,g,b,w,k} | 'none'}
// for Marker
// "Marker" = {"none", "o", "+", "*", ".", "x", "s", "d", "^", "v", ">", "<", 'p', 'h'}
// "MarkerEdgeColor" = 'auto' (default) | 'none' | RGB triplet | {y,m,c,r,g,b,w,k}
// "MarkerFaceColor" = 'auto' (default) | 'none' | RGB triplet | {y,m,c,r,g,b,w,k}
// "MarkerSize" = 6
template<class TMatX = Eigen::MatrixXf, class TMatY = Eigen::MatrixXf>
int plot(const TMatX &X, const TMatY &Y,
string nm0 = "",
string nm1 = "", string nm2 = "",
string nm3 = "", string nm4 = "",
string nm5 = "", string nm6 = "",
string nm7 = "", string nm8 = "",
string nm9 = "", string nm10 = "",
string nm11 = "", string nm12 = "",
string nm13 = "", string nm14 = ""
){
MatArray MX, MY;
MX.copy_from_eigen(X); MX.put(_engine, "MX");
MY.copy_from_eigen(Y); MY.put(_engine, "MY");
string plot_code = "MX, MY";
string code;
#define EVL_CODE(_ARG0,_ARG1) code = var_plot_code(nm##_ARG0, nm##_ARG1); if(code !