如何在Matlab中将字符串保存到文件中时禁用反斜杠转义?

Dim*_*ims 1 matlab escaping backslash

以下两行都没有将"a\b"写入文件

fid = fopen('myfile.txt','w'); fprintf(fid, 'a\b'); fclose(fid);

fid = fopen('myfile.txt','wb'); fprintf(fid, 'a\b'); fclose(fid);
Run Code Online (Sandbox Code Playgroud)

也许,Matlab在保存到文件期间会进行反斜杠转义.

如何禁用此"功能"?

字符串应保持完整,即fprintf(fid, 'a\\b')不是解决方案,length('a\b')==3应该是true.

geh*_*eis 5

您可以使用转换字符fprintf,即%s在这种情况下

fid = fopen('myfile.txt','w'); fprintf(fid, '%s', 'a\b'); fclose(fid);
Run Code Online (Sandbox Code Playgroud)

length('a\b')==3如果我没有记错的话,你的情况不会像这样违反