SamuelNLP
SamuelNLP

Reputation: 4136

How to run a file.m from qt

I have a file.m, which is a file from matlab, and I want it to run when I run my qt project.

I noticed I could use this -r MATLAB_command - Start MATLAB and execute the MATLAB_command.

and this,

-nodesktop           - Do not start the MATLAB desktop. Use the current
                           terminal for commands. The Java virtual machine
                           will be started.

from the matlab help, but i'm not sure if I'm thinking the right way.

My main.cpp

#include <QtGui/QApplication>
#include <iostream>
using namespace std;
#include <cmath>

#include <QProcess>
#include "planevolume.h"
#include "dialog.h"

int main(int argc, char *argv[])
{
    // Start MATLAB MAIN.m
    QProcess* p = new QProcess(NULL);
    p->start( QString( "/usr/local/MATLAB/R2011b/bin/matlab"),
              QStringList() << QString("-r /home/matt/Desktop/PlaneVolumeExec/MAIN.m")
                            << QString("-nosplash")
                            << QString("-nodesktop"));

    QApplication app(argc, argv);

    Dialog *dialog= new Dialog;

    if (dialog->exec())
    {
        planevolume mainwindow(dialog->getdirprefix(),dialog->getxpax(), dialog->getypax(), dialog->getzpax(), dialog->getxmmax(), dialog->getymmax(), dialog->getzmmax(), dialog->getintzminp(), dialog->getintzmaxp(), dialog->getintzminm(), dialog->getintzmaxm());
        mainwindow.show();
        return app.exec();
    }

return 0;
}

Matlab help

/*-h|-help             - Display arguments.
    -n                   - Display final environment variables,
                           arguments, and other diagnostic
                           information. MATLAB is not run.
    -e                   - Display ALL the environment variables and
                           their values to standard output. MATLAB
                           is not run. If the exit status is not
                           0 on return then the variables and values
                           may not be correct.
    -arch                - Start MATLAB assuming architecture arch.
    v=variant            - Start the version of MATLAB found
                           in bin/glnxa64/variant instead of bin/glnxa64.
    v=arch/variant       - Start the version of MATLAB found
                           in bin/arch/variant instead of bin/glnxa64.
    -c licensefile       - Set location of the license file that MATLAB
                           should use.  It can have the form port@host or
                           be a colon separated list of license files.
                           The LM_LICENSE_FILE and MLM_LICENSE_FILE
                           environment variables will be ignored.
    -display Xdisplay    - Send X commands to X server display, Xdisplay.
    -nodisplay           - Do not display any X commands. The MATLAB
                           desktop will not be started. However, unless
                           -nojvm is also provided the Java virtual machine
                           will be started.
    -nosplash            - Do not display the splash screen during startup.
    -mwvisual visualid   - The default X visual to use for figure windows.
    -debug               - Provide debugging information especially for X
                           based problems.
    -desktop             - Allow the MATLAB desktop to be started by a
                           process without a controlling terminal. This is
                           usually a required command line argument when
                           attempting to start MATLAB from a window manager
                           menu or desktop icon.
    -nodesktop           - Do not start the MATLAB desktop. Use the current
                           terminal for commands. The Java virtual machine
                           will be started.
    -nojvm               - Shut off all Java support by not starting the
                           Java virtual machine. In particular the MATLAB
                           desktop will not be started.
    -jdb [port]          - Enable remote Java debugging on port (default 4444)
    -r MATLAB_command    - Start MATLAB and execute the MATLAB_command.
    -logfile log         - Make a copy of any output to the command window
                           in file log. This includes all crash reports.
    -Ddebugger [options] - Start debugger to debug MATLAB.*/

Upvotes: 1

Views: 1413

Answers (4)

LordBaboy Jorge
LordBaboy Jorge

Reputation: 11

Try this. It works.

QString program = "C:\\Program Files\\MATLAB\\R2017a\\bin\\matlab.exe";
myProcess->start(program, QStringList() << QString("-nodisplay")<< QString("-nodesktop")<<  QString("-nosplash")<<QString("-r")<<QString("run('C:/Users/eleazar.balbon/Documents/MATLAB/Sample.slx');"));
myProcess->waitForFinished();

Upvotes: 1

Andres Echeverri
Andres Echeverri

Reputation: 13

It might be too late. But, for future references. This is what you should do.

In my script, I'm sending a path that is a variable in mine .m file called ae_run. This is how I have done it.

QProcess* p = new QProcess(NULL);
QString matlab_exe ="matlab /r -nosplash -nodesktop";
QString fileName_path=matlab_exe +" "+ "rec_path='"+path+"\';ae_run";
p->start(fileName_path);
qDebug()<<p->state()<<endl;

rec_path is the variable that I parse to my code, and the fileName is the path. At the end, when I run it. Looks like this.

"matlab /r -nosplash -nodesktop rec_path='D:\\robot_IMU_data\\file_data.txt';ae_run"

If you see, it is like running it from the command line

Upvotes: 0

Ruslan F.
Ruslan F.

Reputation: 5776

The answer below may not work beacuse QProcess added quotes to all parametrs in QStringList You shuld try QProcess::setNativeArguments ( const QString & arguments )

If this also worn't work you can try QProcess::execute ( const QString & program ) [static]

QProcess::execute("My_programm.bin -arg1 val1 -arg2 val2");

I think this is the best way of running uncotrolled external processes. If you create a QProcess instance and dont delete it manualy it well get you program running until of preces exits. Or if you app exits with help of kill or somethimg else you will have a qDebug issue of deleting of uncompleted proces(dont remeber exect string of this issue in qDebug). In different OS this may cause crush of your app(for example i have this problem on Windows XP)

Upvotes: 0

Pie_Jesu
Pie_Jesu

Reputation: 1914

QProcess* p = new QProcess( this );
p->start( "%MATHLAB_EXE_FILE_FULL_PATH%", "%FILE_M_FULL_PATH%" );

so it will be like:

p->start( QString( "C:/Program Files/MatLab 9.0/matlab.exe" ),
          QStringList() << QString( "-r D:/My files/matlab/file.m" )
                        << QString( "-nosplash" ) );

Upvotes: 1

Related Questions