登录论坛

查看完整版本 : 我需要对这个MATLAB字符串中的字符进行转义吗?


poster
2019-12-10, 20:30
我想在MATLAB中调用以下bash命令:

grep "Up to" ~/test_linux/vision1.1/log | awk '{print $7}' 我在MATLAB中使用system() ,但事实证明有错误:

>> [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. 我是否需要在MATLAB中将bash命令中的一些特殊字符作为字符串转义?



回答:

这应该工作:

[status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk ''{print $7}'' '); 你必须逃离'与另一个'如果你想让它显示为一个字符串的字符。对于在MATLAB中 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strings.html)处理字符串 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strings.html) , '是唯一具有特殊含义的字符(它开始和结束字符串),因此它是唯一需要转义的字符。

警告:某些函数可能以不同的方式解释其字符串参数,因此要求某些字符以不同的方式转义。这些要求将出现在每个功能的文档中。这些类型的功能中有一些是我无法想到的:


FPRINTF (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html) / SPRINTF (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sprintf.html) : format参数中出现的%和\字符必须分别转义为%%和\\ 。
REGEXP / REGEXPI (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexpi.html) :在正则表达式中具有特殊含义的字符 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html#f0-42808)必须以\ 开头 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexpi.html) 。


更多&回答... (https://stackoverflow.com/questions/2045197)