IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]C++ 调用Matlab画图

    bendanban发表于 2016-05-01 21:53:31
    love 0

    劳动节闲来无事,写了一天程序,just for fun.

    看,这是C++调用Matlab画图的一段程序。暂时不想多解释了,有兴趣的话,看看下面的代码吧。

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <cmath>
    #include <ctime>
    using namespace std;
    
    
    #include <Eigen/Eigen>
    #include <engine.h>
    
    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*emat.rows()] = emat(r, c);
                }
            }
        }
        inline double& operator[](int i){
            return ptr()[i];
        }
    
    private:
        mxArray *_data;
    };
    
    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;
        }
    
        template<class TMatX = Eigen::MatrixXf, class TMatY = Eigen::MatrixXf>
        int plot(const TMatX &X, const TMatY &Y, string line_spec = "", int figure_id = 1, bool hold_on = false){
            MatArray MX, MY, ID;
            MX.copy_from_eigen(X); MX.put(_engine, "MX");
            MY.copy_from_eigen(Y); MY.put(_engine, "MY");
            ID.resize(1, 1); ID[0] = figure_id; ID.put(_engine, "ID");
    
            string plot_code = " figure(ID); plot(MX, MY";
            if (line_spec != ""){
                plot_code = plot_code + "," + line_spec;
            }
            plot_code += ");";
            plot_code += hold_on ? "hold on;" : "hold off;";
    
            return engEvalString(_engine, plot_code.c_str());
        }   
    
        int exec(string cmd){
            return engEvalString(_engine, cmd.c_str());
        }
    
    private:
        Engine *_engine;
    };
    
    string rndcolor(){
        string color = "[";
        color += to_string((rand() % 256) / 255.) + ",";
        color += to_string((rand() % 256) / 255.) + ",";
        color += to_string((rand() % 256) / 255.) + "]";
        return color;
    }
    
    int main(int argc, char** argv){
        Eigen::MatrixXf data;
    
        srand((unsigned int)time(0));
        data = data.Random(1000, 2);
    
        Matlab mateng;
        //mateng.exec("clear all; close all;");
        mateng.plot(data.col(0), data.col(1), string("'s',") + "'MarkerEdgeColor'," + rndcolor() + ",'MarkerFaceColor'," + rndcolor() + ", 'MarkerSize', 5", 1, true);
    
        return EXIT_SUCCESS;
    }

    执行一下!看看结果!

    这里写图片描述



沪ICP备19023445号-2号
友情链接