1

我有一个 file.m,它是来自 matlab 的文件,我希望它在我运行我的 qt 项目时运行。

我注意到我可以使用这个-r MATLAB_command - Start MATLAB and execute the MATLAB_command.

和这个,

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

来自matlab的帮助,但我不确定我的想法是否正确。

我的 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 帮助

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

4 回答 4

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

所以它会像:

p->start( QString( "C:/Program Files/MatLab 9.0/matlab.exe" ),
          QStringList() << QString( "-r D:/My files/matlab/file.m" )
                        << QString( "-nosplash" ) );
于 2012-09-11T14:31:51.697 回答
1

尝试这个。有用。

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();
于 2017-07-05T01:05:17.780 回答
0

下面的答案可能不起作用,因为 QProcess 为 QStringList 中的所有参数添加了引号您应该尝试QProcess::setNativeArguments ( const QString & arguments )

如果这也不起作用,您可以尝试QProcess::execute ( const QString & program ) [静态]

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

我认为这是运行不受控制的外部进程的最佳方式。如果您创建一个 QProcess 实例并且不手动删除它,那么它可以很好地让您的程序运行,直到 preces 退出。或者,如果您的应用程序在 kill 或 somethimg 的帮助下退出,您将遇到删除未完成进程的 qDebug 问题(不要记住在 qDebug 中执行此问题的字符串)。在不同的操作系统中,这可能会导致您的应用程序崩溃(例如,我在 Windows XP 上遇到此问题)

于 2012-09-11T17:31:57.510 回答
0

可能为时已晚。但是,供以后参考。这是你应该做的。

在我的脚本中,我发送的路径是我的 .m 文件中名为 ae_run 的变量。我就是这样做的。

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 是我解析为我的代码的变量,而 fileName 是路径。最后,当我运行它时。看起来像这样。

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

如果你看到,这就像从命令行运行它一样

于 2017-03-24T17:52:18.963 回答