简体   繁体   English

使用QProcess从Qt调用Matlab函数

[英]Call matlab function from Qt using QProcess

I need to call Matlab function from Qt . 我需要从Qt调用Matlab函数。 I know that there are standard way to do it via Engine , but I was not able to connect .lib libraries (I think because I use Mingw compiler). 我知道有通过Engine执行此操作的标准方法,但是我无法连接.lib库(我想是因为我使用Mingw编译器)。 So, as I understand QProcess is the only way to do it. 因此,据我了解, QProcess是唯一的方法。 I have studied examples and wrote simple program, which has one QLineEdit (for Matlab script) and two QPushButton (for send script to Matlab and read response). 我研究了示例并编写了简单的程序,其中包含一个QLineEdit (用于Matlab脚本)和两个QPushButton (用于将脚本发送到Matlab并读取响应)。 Here is code: 这是代码:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
   {
        ui->setupUi(this);
        proc = new QProcess(this);
        proc->start("\"C:\\Program Files\\MATLAB\\R2013b\\bin\\matlab.exe\"");

    }

   MainWindow::~MainWindow()
   {
       delete ui;
   }

  void MainWindow::on_readButton_clicked()
  {
       QByteArray dataError = proc->readAllStandardError();
       qDebug()<<dataError;
       QByteArray dataOutput = proc->readAllStandardOutput();
       qDebug()<<dataOutput;
  }

       void MainWindow::on_writeButton_clicked()
  {
       QString text = ui->textForMatlab->text();
       QByteArray script;
       script.append(text);
       qDebug()<<script;
       proc->write(script);
   }

When I start that program matlab.exe is launching. 当我启动该程序时,matlab.exe将启动。 But when I type something in QLineEdit and click write button there is no response from matlab. 但是,当我在QLineEdit键入内容并单击“写入”按钮时,matlab没有任何响应。 Could you tell me what I am doing wrong? 你能告诉我我在做什么错吗?

try this 尝试这个

  QString program = "C:/Program Files/MATLAB/R2013b/bin/matlab";
    QStringList arguments;
    arguments << "yourarg" << "youragr2";// if u have any aruguments then pass here

    QProcess *myProcess = new QProcess(this);
    myProcess->start(program, arguments);

if it is not working then try with myProcess->startDetached(program,argumets); 如果它不起作用,则尝试使用myProcess->startDetached(program,argumets);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM