alvarezcl
alvarezcl

Reputation: 579

Calling MATLAB through Shell with Python Script Issues

I'm having an issue with this call I can't seem to resolve:

os.system('matlab -nodisplay -nosplash -r \"processFates;dlmwrite(\'' + FWHM + ' ' + volume      + ' ' + key + ' ' + str(numTrials) + '\", min_timing)\"')

FWHM, volume, key are all strings. I keep getting the error of an extra ) but it seems like I need all of them here.

Upvotes: 1

Views: 255

Answers (1)

mata
mata

Reputation: 69012

Let's try this with some values:

>>> FWHM, volume, key, numTrials, min_timing = 'a', 'b', 'c', 'd', 'e'
>>> print('matlab -nodisplay -nosplash -r \"processFates;dlmwrite(\'' + FWHM + ' ' + volume      + ' ' + key + ' ' + str(numTrials) + '\", min_timing)\"')
matlab -nodisplay -nosplash -r "processFates;dlmwrite('a b c d", min_timing)"

See that double quote after the d? That should probably be a single quote. Also, the min_timing you're passing is the literal string min_timing, not the variable you're expecting.

Using subprocess.Popen instead of os.system you can avoid some of these problems with escaping by not relying on the shell and passing the arguments as strings directly:

command = "processFates; dlmwrite('%s %s %s %s', %s)" % (FWHM, volume, key, numTrials, min_timing)
proc = subprocess.Popen(['matlab', '-nodisplay', '-nosplash', '-r', command])

Upvotes: 2

Related Questions