Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从qt运行文件.m_Qt_Matlab - Fatal编程技术网

如何从qt运行文件.m

如何从qt运行文件.m,qt,matlab,Qt,Matlab,我有一个文件.m,这是一个来自matlab的文件,我希望在运行我的qt项目时运行它 我注意到我可以使用这个-r MATLAB_命令-启动MATLAB并执行MATLAB_命令。 而这个, -nodesktop - Do not start the MATLAB desktop. Use the current terminal for commands. The Java virtual machine

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

我注意到我可以使用这个
-r MATLAB_命令-启动MATLAB并执行MATLAB_命令。

而这个,

-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;
}
所以它会像:

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

QStringList()下面的答案可能不起作用,因为QProcess在QStringList的所有参数中添加了引号 你应该试试

如果这也不起作用,你可以试试

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

可能太晚了。但是,为了将来的参考,这是你应该做的

在我的脚本中,我正在发送一个路径,该路径是我的.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;
如果您看到了,这就像从命令行运行它一样。试试这个。它可以工作

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();
QString program=“C:\\program Files\\MATLAB\\R2017a\\bin\\MATLAB.exe”;

myProcess->start(程序,QStringList()
this
-指向对象的指针,您在其中调用此进程。如果您没有父对象,请发送到那里
NULL
。Aw,我的错误。第二个参数是字符串列表。用法:
p->start(QString(“/usr/local/MATLAB/R2011b/bin/MATLAB”),QStringList()嗯,它没有给我一个错误,但是matlab文件.m不起作用。这个命令应该打开你的文件。我假设,第一个参数会打开它。我在linux上没有matlab,所以不能告诉应该使用什么键来告诉我,应该打开的是文件。它可以是
-f
或其他什么。你能运行
/usr/local/matlab/R吗2011b/bin/matlab--help
在终端中,看看是否有这样的键?
-nosplash
例如,在开始时不显示启动屏幕,所以我更正了我的答案,-nosplash有效,我打开了帮助,没有-f。我试过使用-r,但到目前为止它不起作用。它启动了,但之后它什么都不起作用。我认为是你在理解mathlab中的-r参数时出错。使用-r参数,matlab启动的是命令而不是文件。因此我认为你应该像matlab一样编写smthg-r“run('C:\\mfile.m');“我按照你说的做了,但它不起作用,我是这样写的
QString('/home/samuelpedro/Desktop/PlaneVolumeFinal/PlaneVolumeExec/MAIN.m')
QProcess::execute("My_programm.bin -arg1 val1 -arg2 val2");
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;
"matlab /r -nosplash -nodesktop rec_path='D:\\robot_IMU_data\\file_data.txt';ae_run"
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();