在Matlab中使用sprintf?

在Matlab中使用sprintf?,matlab,printf,Matlab,Printf,这类似于我之前提出的关于在Matlab中打开pdf的问题 file = 'sl3_knt_1_2.pdf' location = 'C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe %s' str = sprintf(location,file); system(str) 这将返回警告: Warning: Invalid escape sequence appears in format string. See help sp

这类似于我之前提出的关于在Matlab中打开pdf的问题

file = 'sl3_knt_1_2.pdf'
location = 'C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe %s'
str = sprintf(location,file);
system(str)
这将返回警告:

Warning: Invalid escape sequence appears in format string. See help sprintf for valid escape sequences. 
我认为它与作为转义序列读取的位置变量有关,因为它使用\但我不确定。我好像没法让它工作

试试这个:

file = 'sl3_knt_1_2.pdf'
location = 'C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe'

str = sprintf('%s %s',location, file)

system(str)

或者,您可以更改位置字符串,如下所示:

location = 'C:\\Program Files\\Tracker Software\\PDF Viewer\\PDFXCview.exe %s'

通常
\
用于特殊字符。例如,
\n
是行尾。因此,当您真的想编写
\
时,需要使用
\
对其进行转义。因此,您需要编写
\
在这种情况下

简单的解决方案是使用“/”而不是“\”,它适用于所有平台,包括Windows作为一个特殊的角色是有问题的