Do I need to escape characters in this MATLAB string?

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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:

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:



回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!