Do I need to escape characters in this MATLAB string?


Question

I would like to call the following bash command in MATLAB:

grep "Up to" ~/test_linux/vision1.1/log | awk '{print $7}'

I use system() in MATLAB, but it turns out to have errors:

>> [status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk '{print $7}' ');     
??? [status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk '{print $7}' ');  

Error: Unbalanced or unexpected parenthesis or bracket.

Do I need to escape some special characters in the bash command as a string in MATLAB?

1
2
1/20/2010 7:02:31 PM

Accepted Answer

This should work:

[status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk ''{print $7}'' ');

You have to escape ' with another ' if you want it to appear as a character in a string. With respect to handling strings in MATLAB, ' is the only character with special meaning (it starts and ends the string), so it is the only one that needs escaping.

Caveat: Some functions may interpret their string arguments in different ways, and thus require certain characters to be escaped in different ways. These requirements will appear in the documentation for each function. A couple of these types of functions off the top of my head:

13
1/11/2010 11:42:32 PM

You'll need to escape the single quotes in the command string. Otherwise MATLAB will interpret them as the end of the string, and then break down on the stuff that follows it.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon