PDA

View Full Version : To call a string containing file path in a command



Nick_Neo
9th June 2015, 06:34
void MainWindow::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open"),
"S://MATLAB/",
"MATLAB Code files (*.m);;All Files (*.*)");
QProcess* p = new QProcess(this);
p->start("C:/\"Program Files\"/MATLAB/R2013a/bin/matlab.exe -nosplash -nojvm -r run('filename');");
}


But I'm unable to use the string filename to run MATLAB script file. Instead of sending the file path like "S:/MATLAB/test.m" , the variable name "filename" is being executed in the run command. I have to manually enter the file path. Is there any other way to redirect the file path to run it directly on a mouse-click.?

stampede
9th June 2015, 09:32
You need to use the value of variable 'filename' and not the string constant "filename".


p->start( QString("C:/\"Program Files\"/MATLAB/R2013a/bin/matlab.exe -nosplash -nojvm -r run('%1');").arg(filename) );



void function(const QString& s);
...
QString string = ...;
...
function(string);
...
function("string");

Do you understand the difference between the two above function calls ?

Nick_Neo
9th June 2015, 09:41
Now I get it. Thank you

Nick_Neo
9th June 2015, 12:04
But what if my filename was S:/MATLAB files/test.m
In console window of MATLAB only the first part of the command i.e., run('S:/MATLAB is being executed giving an error :A MATLAB string constant is not terminated properly.

Lesiok
9th June 2015, 12:33
Use another variant of start method :

QString app( "C:/Program Files/MATLAB/R2013a/bin/matlab.exe" );
QStringList arguments;
arguments << "-nosplash" << "-nojvm" << "-r" << QString("run('%1')").arg(filename);
p->start(app,arguments);

wysota
9th June 2015, 12:34
You need to use proper shell escaping or use a variant of QProcess::start() which accepts arguments in separate strings.