[Solved] Running MATLAB in QT-GUI



  • Hi, every one....

    In my project I want to run MATLAB program in my GUI. In MATLAB the program is like reading data from some Address location and that data will be displayed(plotting the waveform). In my searching I get 2 solutions 1). Using QProcess function calling the MATLAB program
    2). Using MATLAB Engine library in GUI code,

    I am confused with these two things which one suits for my Project.
    If any one knows please help me



  • If you have enough time and skills the second way is the best (least overhead, maximum compatibility with matlab).
    Using QProcess is like your GUI mimic user interaction with matlab console. That is you have to send proper-formatted matlab script through QProcess to matlab executable and then read response from it (also processing string data).
    Using QProcess allows you to pin octave egine instead of matlab. QtOctave is an example.



  • Thank you for the reply.
    Can you give any example code for this

    bq. Using QProcess is like your GUI mimic user interaction with matlab console.

    I didn't get this one Could you explain me



    1. I never worked with that. you should search in matlab documentation.
    2. "QProcess":http://qt-project.org/doc/qt-4.8/qprocess.html is well documented. There is example for synchronous execution.

    You can write some warrper to manage asynchronous calls and responses.

    @class MatLabProc
    {
    QProcess proc;

    enum State {IDLE, ERROR, CALCSIN, ...};
    State state;

    public:
    MatLabProc() {
    state = IDLE;
    }

    //start process
    bool start() {
    proc.start("matlab.exe");
    return proc.waitForStarted();
    }

    //stop process
    bool stop() {...}

    bool calcSin(const QVector<double> & vec) {
    if (state != IDLE) return false;
    state = CALCSIN;

    QByteArray script = "clear;\n"
    "x = [.. create from vec ...];\n"
    "y = sin(x);\n"
    "y\n";

    proc.write(script);
    proc.closeWriteChannel();
    connect(&proc, SIGNAL(readyReadStandardOutput()), SLOT(sin_stream()));
    connect(&proc, SIGNAL(readyReadStandardError()), SLOT(sin_err()));

    return true;
    }

    signals:
    void sinFinished(QVector<double> data);

    private slots:
    void sin_stream() {
    /* Ouptut like this
    y =

    Columns 1 through 6:

    1.5932e-01 7.9915e-02 3.9989e-02 1.9999e-02 1.3000e-02 6.9999e-03

    Columns 7 through 10:

    4.0000e-03 2.0000e-03 1.0000e-03 8.0000e-04
    */
    if (proc.canReadLine()) {
    QByteArray data = proc.readAllStandardOutput();
    //read numbers from data
    if (.. 10 number read ..) {
    proc.closeReadChannel();
    proc.disconnect();
    state = IDLE;
    emit sinFinished(vecOfSinResults);
    }
    }

    }

    void sin_err() {
    if (proc.canReadLine()) {
    QByteArray data = proc.readAllStandardError();
    //process errors in data
    proc.closeReadChannel();
    proc.disconnect();
    state = ERROR;
    }
    }
    };@



  • Thank you, I will try my level best, If I get any doubts I will post again. sorry can you give any example code for calling MATLAB engine function from QT-Creator

    And I have one doubt regarding MATLAB engine, if I use this in QT, It will execute only Matlab commands or it will run the Matlab.exe file?

    Because I want to Plot the waveforms of some data using Matlab & It is running from GUI. and I found that this MATLAB engine is called from C/C++ files or from visual studio only.
    Is it possible to call from QT GUI programming



  • Running matlab engine examples:
    "YouTube video":http://www.youtube.com/watch?v=3zdVQf1MotY (plotting example)
    "Presentation about matlab API":http://goo.gl/jwFYa

    If you use QProcess than matlab will run as separate process. If you use offical c++ API propably too (I'm not sure).

    bq. Is it possible to call from QT GUI programming

    Yes, the plots will pop up in separate windows (in matlab's figure plotting tool). If you want to integrate plots with Qt Gui forms then i suggest you to use "Qwt library":http://qwt.sourceforge.net/ .



  • bq. and I found that this MATLAB engine is called from C/C++ files or from visual studio only.

    matab only ships liberties compatible with visual studio compiler so you hawe to use qt sdk compiled with vs2010.



  • hi thank you for giving reply.

    plotting from matlab and qt is different. Both of having in my project. I am using QProcess now. If I get any difficulties when I will testing the project I will modify the code


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.