How can I print a string that contains 'Escape characters'?

283 ビュー (過去 30 日間)
Michael Jarboe
Michael Jarboe 2016 年 1 月 12 日
コメント済み: Michael Jarboe 2016 年 1 月 12 日
I am copying data from a file and attempting to print it to a second file. One of the lines of data I want to copy is a path to a folder. C:\ni-rt\NIVeriStand\XNET\Raw Data Logs '\n' and '\N' are escape characters when using fprintf(fid2, mystring) When I open the write file the data looks like
C:
i-rt
Is there a way I can print the string as is to a second file?

回答 (1 件)

Guillaume
Guillaume 2016 年 1 月 12 日
編集済み: Guillaume 2016 年 1 月 12 日
The simplest way is not to use the string you want to write as the format string, but as one of the argument to be formatted:
fprintf(fid2, '%s', mystring);
Alternatively, you could escape the '\':
fprintf(fid2, strrep(mystring, '\', '\\'));
Option 1 is more robust and should be faster.
  1 件のコメント
Michael Jarboe
Michael Jarboe 2016 年 1 月 12 日
Thank you Guillaume!

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by