[英]How to include double quote in Matlab's system() command
I need to pass filename as input to a powershell command which includes spaces.我需要将文件名作为输入传递给包含空格的 powershell 命令。 Hence I am hoping to include double quotes.因此,我希望包括双引号。
However, Matlab "eats" all double quotes in the input of system()
when passing arguments to powershell.但是,当将 arguments 传递给 powershell 时,Matlab “吃掉”了system()
输入中的所有双引号。
For example, note how the examples below all the the same output.例如,请注意下面的示例如何都相同 output。
>> system('powershell.exe echo a c','-echo')
a
c
ans =
0
>> system('powershell.exe echo "a c"','-echo')
a
c
ans =
0
>> system('powershell.exe echo ""a c""','-echo')
a
c
ans =
0
>> system(['powershell.exe echo ',char(34),'a c',char(34)],'-echo')
a
c
ans =
0
The actual output for echo "ac"
in powershell is a c
in a single line. powershell 中echo "ac"
的实际 output 是单行中a c
。 The change of line only occurs without double quotes.换行只发生在没有双引号的情况下。
Just for experiment, I also tried ""ac""
and the expected input is the same as change line, a, change line, c.只是为了实验,我还尝试了""ac""
,预期的输入与更改线相同,a,更改线,c。 With the return, it seems that any and all double quotes are "eaten" alive by Matlab.随着返回,似乎所有双引号都被 Matlab “吃掉”了。
How do I bring the double quotes back when using system()
?使用system()
时如何带回双引号?
It is PowerShell that is eating your double quotes:吃掉你的双引号的是PowerShell :
You're passing a command (piece of PowerShell code) to the PowerShell CLI , via the -Command
( -c
) parameter (which is positionally implied in your case).您正在通过-Command
( -c
) 参数(在您的案例中隐含位置)将命令(一段 PowerShell 代码)传递给 PowerShell CLI 。
"
characters that should be considered part of the command must be escaped as \"
(sic) "
应被视为命令一部分的字符必须转义为\"
(原文如此)
"
don't work is that PowerShell considers them to have syntactic function on the command line only - they are simply stripped after all arguments have been parsed; the resulting tokens are then joined with spaces, and the resulting string finally interpreted as PowerShell code.未转义的"
不起作用的原因是 PowerShell 认为它们仅在命令行上具有语法 function - 在所有 arguments 已被解释后,它们被简单地剥离;并解析结果字符串,最终解释为作为 PowerShell 代码。 While using just \"
in your command would fix the problem, it is advisable to also enclose the entire command being passed in "..."
, because that prevents potentially unwanted whitespace normalization .虽然在您的命令中仅使用\"
可以解决问题,但建议也将传递的整个命令包含在"..."
中,因为这样可以防止可能不需要的空白规范化。
system('powershell.exe " echo \"a c\" "', '-echo')
Caveat :警告:
Since MatLab's system()
function executes the given command line via cmd.exe
(which is inefficient in your case, since you don't need shell functionality), use of \"
can break the invocation, due to how cmd.exe
's parses command lines. Since MatLab's system()
function executes the given command line via cmd.exe
(which is inefficient in your case, since you don't need shell functionality), use of \"
can break the invocation, due to how cmd.exe
's解析命令行。
To avoid edge cases when cmd.exe
is involved , enclose the overall command in "...."
and escape pass-through "
as follows:为避免涉及cmd.exe
时出现极端情况,请将整个命令括在"...."
中并转义通过"
,如下所示:
"^""
(sic) when calling powershell.exe
(the Windows PowerShell CLI)调用powershell.exe
时使用"^""
(原文如此)( Windows PowerShell CLI)""
when calling pwsh.exe
(the PowerShell (Core) 7+ CLI).调用pwsh.exe
( PowerShell (Core) 7+ CLI)时使用""
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.