Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. QT and Matlab

QT and Matlab

Scheduled Pinned Locked Moved Solved 3rd Party Software
10 Posts 7 Posters 4.5k Views
  • 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.
  • A Offline
    A Offline
    alberto.brunero
    wrote on 2 Dec 2019, 11:23 last edited by
    #1

    Hi to all,
    I am trying to use QT and Matlab together, but I can not find anything useful for my purpose.
    Is there any kind of guide to do this? I have no idea in how to setup the pro, the .h and what to write in the cpp.
    I just want to execute a script of matlab in my QT program, sending data from the GUI to the input of the function.m and plot the result. It would be good even if I could get some data, processed by matlab, back to my QT program.
    Thanks.

    J 1 Reply Last reply 2 Dec 2019, 12:47
    0
    • A alberto.brunero
      2 Dec 2019, 11:23

      Hi to all,
      I am trying to use QT and Matlab together, but I can not find anything useful for my purpose.
      Is there any kind of guide to do this? I have no idea in how to setup the pro, the .h and what to write in the cpp.
      I just want to execute a script of matlab in my QT program, sending data from the GUI to the input of the function.m and plot the result. It would be good even if I could get some data, processed by matlab, back to my QT program.
      Thanks.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 2 Dec 2019, 12:47 last edited by
      #2

      @alberto-brunero Take a look at https://www.mathworks.com/solutions/matlab-and-c.html
      And for QMake: https://doc.qt.io/qt-5/third-party-libraries.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 2 Dec 2019, 15:28
      3
      • J jsulm
        2 Dec 2019, 12:47

        @alberto-brunero Take a look at https://www.mathworks.com/solutions/matlab-and-c.html
        And for QMake: https://doc.qt.io/qt-5/third-party-libraries.html

        A Offline
        A Offline
        alberto.brunero
        wrote on 2 Dec 2019, 15:28 last edited by
        #3

        @jsulm I am just trying to follow these sources, but when I add engOpen the program crash at the very start (not after I push my button) giving me simply "The program has unexpectedly finished."

        Here my .pro file

        QT       += core gui
        
        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
        
        CONFIG += c++11
        
        # The following define makes your compiler emit warnings if you use
        # any Qt feature that has been marked deprecated (the exact warnings
        # depend on your compiler). Please consult the documentation of the
        # deprecated API in order to know how to port your code away from it.
        DEFINES += QT_DEPRECATED_WARNINGS
        
        # You can also make your code fail to compile if it uses deprecated APIs.
        # In order to do so, uncomment the following line.
        # You can also select to disable deprecated APIs only up to a certain version of Qt.
        #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
        
        SOURCES += \
            main.cpp \
            mainwindow.cpp
        
        HEADERS += \
            mainwindow.h
        
        FORMS += \
            mainwindow.ui
        
        LIBS += -L"C:\Program Files\MATLAB\R2019b\extern\lib\win64\mingw64" -llibeng \
                -L"C:\Program Files\MATLAB\R2019bb\extern\lib\win64\mingw64" -llibmx
        
        INCLUDEPATH += "C:\Program Files\MATLAB\R2019b\extern\include"
        
        # Default rules for deployment.
        qnx: target.path = /tmp/$${TARGET}/bin
        else: unix:!android: target.path = /opt/$${TARGET}/bin
        !isEmpty(target.path): INSTALLS += target
        
        

        My mainwindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class MainWindow; }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        private slots:
            void on_pushButton_clicked();
        
        private:
            Ui::MainWindow *ui;
        };
        #endif // MAINWINDOW_H
        
        

        and my mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "engine.h"
        #include "matrix.h"
        #include <QDebug>
        
        #define  BUFSIZE 256
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        
        void MainWindow::on_pushButton_clicked()
        {
            Engine *ep;
            mxArray *T = NULL, *result = NULL;
            char buffer[BUFSIZE+1];
            double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
            //Call engOpen with a NULL string. This starts a MATLAB process
            //on the current host using the command "matlab".
            if (!(ep = engOpen(""))) {
                qDebug() << "\nCan't start MATLAB engine\n";
                exit(-1);
            }
        
            //Send data to MATLAB, analyze the data, and plot the result.
            //Create a variable for our data
            T = mxCreateDoubleMatrix(1, 10, mxREAL);
            memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
            //Place the variable T into the MATLAB workspace, write last argument in the engine, giving the name of the second argument.
            engPutVariable(ep, "T", T);
            //Evaluate the function in the given engine.
            engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
            //Plot the result
            engEvalString(ep, "plot(T,D);");
            engEvalString(ep, "title('Position vs. Time for a falling object');");
            engEvalString(ep, "xlabel('Time (seconds)');");
            engEvalString(ep, "ylabel('Position (meters)');");
            //use fgetc() to make sure that we pause long enough to be able to see the plot
            printf("Hit Enter to continue\n\n");
            fgetc(stdin);
            //Wait until you press enter
            printf("Done for Part I.\n");
            //Destroy the array and close the MATLAB engine.
            mxDestroyArray(T);
            engEvalString(ep, "close;");
        }
        
        

        So, what am I missing?

        J 1 Reply Last reply 3 Dec 2019, 05:15
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Dec 2019, 18:20 last edited by
          #4

          Hi,

          The matlabs dlls are likely not found on run. Go to the Run part of the Project panel and add the folder where they are located to the PATH environment variable.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply 3 Dec 2019, 11:44
          3
          • A alberto.brunero
            2 Dec 2019, 15:28

            @jsulm I am just trying to follow these sources, but when I add engOpen the program crash at the very start (not after I push my button) giving me simply "The program has unexpectedly finished."

            Here my .pro file

            QT       += core gui
            
            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
            
            CONFIG += c++11
            
            # The following define makes your compiler emit warnings if you use
            # any Qt feature that has been marked deprecated (the exact warnings
            # depend on your compiler). Please consult the documentation of the
            # deprecated API in order to know how to port your code away from it.
            DEFINES += QT_DEPRECATED_WARNINGS
            
            # You can also make your code fail to compile if it uses deprecated APIs.
            # In order to do so, uncomment the following line.
            # You can also select to disable deprecated APIs only up to a certain version of Qt.
            #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
            
            SOURCES += \
                main.cpp \
                mainwindow.cpp
            
            HEADERS += \
                mainwindow.h
            
            FORMS += \
                mainwindow.ui
            
            LIBS += -L"C:\Program Files\MATLAB\R2019b\extern\lib\win64\mingw64" -llibeng \
                    -L"C:\Program Files\MATLAB\R2019bb\extern\lib\win64\mingw64" -llibmx
            
            INCLUDEPATH += "C:\Program Files\MATLAB\R2019b\extern\include"
            
            # Default rules for deployment.
            qnx: target.path = /tmp/$${TARGET}/bin
            else: unix:!android: target.path = /opt/$${TARGET}/bin
            !isEmpty(target.path): INSTALLS += target
            
            

            My mainwindow.h

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            
            QT_BEGIN_NAMESPACE
            namespace Ui { class MainWindow; }
            QT_END_NAMESPACE
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
            
            private slots:
                void on_pushButton_clicked();
            
            private:
                Ui::MainWindow *ui;
            };
            #endif // MAINWINDOW_H
            
            

            and my mainwindow.cpp

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include "engine.h"
            #include "matrix.h"
            #include <QDebug>
            
            #define  BUFSIZE 256
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            
            void MainWindow::on_pushButton_clicked()
            {
                Engine *ep;
                mxArray *T = NULL, *result = NULL;
                char buffer[BUFSIZE+1];
                double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
                //Call engOpen with a NULL string. This starts a MATLAB process
                //on the current host using the command "matlab".
                if (!(ep = engOpen(""))) {
                    qDebug() << "\nCan't start MATLAB engine\n";
                    exit(-1);
                }
            
                //Send data to MATLAB, analyze the data, and plot the result.
                //Create a variable for our data
                T = mxCreateDoubleMatrix(1, 10, mxREAL);
                memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
                //Place the variable T into the MATLAB workspace, write last argument in the engine, giving the name of the second argument.
                engPutVariable(ep, "T", T);
                //Evaluate the function in the given engine.
                engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
                //Plot the result
                engEvalString(ep, "plot(T,D);");
                engEvalString(ep, "title('Position vs. Time for a falling object');");
                engEvalString(ep, "xlabel('Time (seconds)');");
                engEvalString(ep, "ylabel('Position (meters)');");
                //use fgetc() to make sure that we pause long enough to be able to see the plot
                printf("Hit Enter to continue\n\n");
                fgetc(stdin);
                //Wait until you press enter
                printf("Done for Part I.\n");
                //Destroy the array and close the MATLAB engine.
                mxDestroyArray(T);
                engEvalString(ep, "close;");
            }
            
            

            So, what am I missing?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 3 Dec 2019, 05:15 last edited by
            #5

            @alberto-brunero To add to @SGaist : on Windows LIBS syntax is different:

            LIBS += "C:/Program Files/MATLAB/R2019b/extern/lib/win64/mingw64/libeng.lib"
            

            Also, you should use / instead of \ in pro files, even on Windows.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            aha_1980A 1 Reply Last reply 17 Feb 2020, 14:13
            3
            • SGaistS SGaist
              2 Dec 2019, 18:20

              Hi,

              The matlabs dlls are likely not found on run. Go to the Run part of the Project panel and add the folder where they are located to the PATH environment variable.

              A Offline
              A Offline
              alberto.brunero
              wrote on 3 Dec 2019, 11:44 last edited by
              #6

              @SGaist @jsulm Thanks to both of you, I have managed to make my program work.

              E 1 Reply Last reply 17 Feb 2020, 11:41
              0
              • A alberto.brunero
                3 Dec 2019, 11:44

                @SGaist @jsulm Thanks to both of you, I have managed to make my program work.

                E Offline
                E Offline
                Estee913
                wrote on 17 Feb 2020, 11:41 last edited by
                #7

                @alberto-brunero I have the same problem. Please share the solution. Thanks.

                1 Reply Last reply
                0
                • J jsulm
                  3 Dec 2019, 05:15

                  @alberto-brunero To add to @SGaist : on Windows LIBS syntax is different:

                  LIBS += "C:/Program Files/MATLAB/R2019b/extern/lib/win64/mingw64/libeng.lib"
                  

                  Also, you should use / instead of \ in pro files, even on Windows.

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 17 Feb 2020, 14:13 last edited by
                  #8

                  @jsulm said in QT and Matlab:

                  @alberto-brunero To add to @SGaist : on Windows LIBS syntax is different:

                  LIBS += "C:/Program Files/MATLAB/R2019b/extern/lib/win64/mingw64/libeng.lib"
                  

                  That is not true. The correct way is to use -L... -l... on Windows too, as Creator then sets up the library search path for you.

                  Also, you should use / instead of \ in pro files, even on Windows.

                  I fully agree to that. Also, by all means avoid spaces in paths.

                  Regards

                  Qt has to stay free or it will die.

                  JKSHJ 1 Reply Last reply 18 Feb 2020, 02:33
                  3
                  • aha_1980A aha_1980
                    17 Feb 2020, 14:13

                    @jsulm said in QT and Matlab:

                    @alberto-brunero To add to @SGaist : on Windows LIBS syntax is different:

                    LIBS += "C:/Program Files/MATLAB/R2019b/extern/lib/win64/mingw64/libeng.lib"
                    

                    That is not true. The correct way is to use -L... -l... on Windows too, as Creator then sets up the library search path for you.

                    Also, you should use / instead of \ in pro files, even on Windows.

                    I fully agree to that. Also, by all means avoid spaces in paths.

                    Regards

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on 18 Feb 2020, 02:33 last edited by JKSH
                    #9

                    @aha_1980 said in QT and Matlab:

                    @jsulm said in QT and Matlab:

                    @alberto-brunero To add to @SGaist : on Windows LIBS syntax is different:

                    LIBS += "C:/Program Files/MATLAB/R2019b/extern/lib/win64/mingw64/libeng.lib"
                    

                    That is not true. The correct way is to use -L... -l... on Windows too, as Creator then sets up the library search path for you.

                    Also, you should use / instead of \ in pro files, even on Windows.

                    I fully agree to that. Also, by all means avoid spaces in paths.

                    Agree with @aha_1980 about -L..., -l..., and / on Windows.

                    Spaces in LIBS are quite safe though; just wrap the path in double quotes:

                    LIBS += \
                        -L"C:/Program Files/MATLAB/R2019b/extern/lib/win64/mingw64/"
                        -llibeng
                    

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      Assaf1990
                      wrote on 13 Dec 2020, 15:06 last edited by
                      #10

                      I have the same problem. what is the the solution. Thanks.
                      I tried LIBS += "C:/Program Files/MATLAB/R2019b/extern/lib/win64/mingw64/libeng.lib"
                      and LIBS +=
                      -L"C:/Program Files/MATLAB/R2019b/extern/lib/win64/mingw64/"
                      -llibeng
                      and still not worked

                      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