QT and Matlab
-
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. -
@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 -
@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?
-
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.
-
@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.
-
@alberto-brunero I have the same problem. Please share the solution. Thanks.
-
@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
-
@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
-