bash 我需要转义这个 MATLAB 字符串中的字符吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2045197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to escape characters in this MATLAB string?
提问by Tim
I would like to call the following bash command in MATLAB:
我想在 MATLAB 中调用以下 bash 命令:
grep "Up to" ~/test_linux/vision1.1/log | awk '{print }'
I use system()in MATLAB, but it turns out to have errors:
我system()在 MATLAB 中使用,但结果是有错误:
>> [status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk '{print }' ');
??? [status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk '{print }' ');
Error: Unbalanced or unexpected parenthesis or bracket.
Do I need to escape some special characters in the bash command as a string in MATLAB?
我是否需要将 bash 命令中的一些特殊字符转义为 MATLAB 中的字符串?
回答by gnovice
This should work:
这应该有效:
[status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk ''{print }'' ');
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.
如果您希望它显示为字符串中的字符,则必须'使用另一个转义'。关于在 MATLAB 中处理字符串,'是唯一具有特殊含义的字符(它开始和结束字符串),因此它是唯一需要转义的字符。
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:
警告:某些函数可能会以不同的方式解释它们的字符串参数,因此需要以不同的方式对某些字符进行转义。这些要求将出现在每个功能的文档中。我脑海中浮现出以下几种类型的功能:
- FPRINTF/SPRINTF:
%and\characters appearing in the formatargument have to be escaped as%%and\\, respectively. - REGEXP/REGEXPI: Characters with special meaningin a regular expression must be preceded with a
\.
- FPRINTF/ SPRINTF:
%和格式参数中\出现的字符必须分别转义为和。%%\\ - REGEXP/REGEXPI:正则表达式中具有特殊含义的字符必须以
\.
回答by Anon.
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.
您需要对命令字符串中的单引号进行转义。否则 MATLAB 会将它们解释为字符串的结尾,然后分解后面的内容。

