views:

1165

answers:

2

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?

+6  A: 

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:

gnovice
Just wonder what is the escape character in Matlab? Is it always single quote? I remember in function printf(), it is \.
Tim
@Tim: I added some more detail about escape characters to the answer.
gnovice
+2  A: 

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.

Anon.