0

I want to run a certain Python script that calls MATLAB through the Ubuntu 12.04 command terminal. The script has this line:

os.system("matlab -nojvm -nodisplay -nosplash -r "ReadFates5mm;quit"")

but it returns a syntax error on the last portion with ReadFates.. and I don't know how to fix it.

I know there's a way using the matlab python bridge but I would have to convert my matlab file into a function.

Thanks!

5
  • 1
    os.system('matlab -nojvm -nodisplay -nosplash -r "ReadFates5mm;quit"') ? OR escape the quotes properly. Commented Jun 28, 2013 at 5:58
  • Got it! What difference does it make to ' ' vs " " though? Commented Jun 28, 2013 at 6:09
  • Added the explanation as answer. Commented Jun 28, 2013 at 6:41
  • @alvarezcl There is absolutely no difference between " and '. The problem is that, to include a literal " inside a string delimited with " you should escape it with a backslash. E.g. "Say \"something\"". The same is true for ': 'Say \'something\''. To avoid using the backslash you can use a different delimiter(e.g. if the string include " you use ' as delimiter, and if the string contains ' you use "). By the way: os.system shouldn't be used. Replace it with subprocess.call. Commented Jun 28, 2013 at 6:53
  • Actually, there is a slight difference between using " & ' in some scripting languages. 'something' is string literal in many of the languages. Any escape character within singly quoted string is taken as literally. Also '$variable' will not be expanded in bash. etc. I am not very sure about usage of ' vs " in python. I just wanted to tell OP, that "absolutely no difference between " and '" is not a generic statement. Commented Jun 28, 2013 at 7:43

1 Answer 1

1

You need to quote the string properly.

Try:

os.system('matlab -nojvm -nodisplay -nosplash -r "ReadFates5mm;quit"')
OR
os.system("matlab -nojvm -nodisplay -nosplash -r 'ReadFates5mm;quit'")

Alternately, you can escape the nested double quotes.

os.system("matlab -nojvm -nodisplay -nosplash -r \"ReadFates5mm;quit\"")

Explanation:

In your code,

 os.system("matlab -nojvm -nodisplay -nosplash -r "ReadFates5mm;quit"")
           1                                      1'                22'

The double quote started at marker 1 ends at the market 1' & the quote started at 2 ends at 2'. Basically, you need to escape the " at 1' & 2, using \".

Alternately, you can use other quote character, '.


For more details, search for "string quoting & escape characters".

http://en.wikipedia.org/wiki/Escape_character#Programming_and_data_formats

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.