用c++函数调用matlab绘制besselj曲线

matlab eigen学习中。可以直接通过engEvalString发送命令,由matlab自己创建数据,自己做图。c++只是唤醒作用。

代码:

/*
 * example3.cpp
 *
 * This is an example showing standard markers available in MATLAB® plots.
 * http://www.mathworks.com/matlabcentral/fileexchange/35231-matlab-plot-gallery-standard-plot-markers/content/html/Standard_Plot_Markers.html
*/

#include <iostream>
#include <math.h>
#include "engine.h"

int main() {

    Engine *ep; //定义Matlab引擎指针。
    if (!(ep=engOpen("\0"))) //测试是否启动Matlab引擎成功。
    {
        std::cout<< "Can't start MATLAB engine"<<std::endl;
        return EXIT_FAILURE;
    }

    // 向matlab发送命令。在matlab内部自己去产生即将绘制的曲线上的数据。
    // Generate some data using the besselj function
    engEvalString(ep, "x = 0:0.2:10;y0 = besselj(0,x);y1 = besselj(1,x);y2 = besselj(2,x);y3 = besselj(3,x);y4 = besselj(4,x);y5 = besselj(5,x);y6 = besselj(6,x);");

    // Plot the points from the Bessel functions using standard marker types figure
    engEvalString(ep, "plot(x, y0, 'r+', x, y1, 'go', x, y2, 'b*', x, y3, 'cx', x, y4, 'ms', x, y5, 'yd', x, y6, 'kv');");


    //Use cin.get() to make sure that we pause long enough to be able to see the plot.
    std::cout<<"Hit any key to exit!"<<std::endl;
    std::cin.get();

    return EXIT_SUCCESS;
}

编译,运行:

g++ example3.cpp -o example3 -leng -lmx

./example3
Hit any key to exit!

运行结果:

用c++函数调用matlab绘制besselj曲线_第1张图片


你可能感兴趣的:(用c++函数调用matlab绘制besselj曲线)