Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Running MATLAB in QT-GUI
Forum Updated to NodeBB v4.3 + New Features

[Solved] Running MATLAB in QT-GUI

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 16.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Swinetha
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • 8 Offline
      8 Offline
      8majkel8
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Swinetha
        wrote on last edited by
        #3

        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 Reply Last reply
        0
        • 8 Offline
          8 Offline
          8majkel8
          wrote on last edited by
          #4
          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;
          }
          }
          };@

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Swinetha
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • 8 Offline
              8 Offline
              8majkel8
              wrote on last edited by
              #6

              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/ .

              1 Reply Last reply
              0
              • 8 Offline
                8 Offline
                8majkel8
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Swinetha
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved