MATLAB Answers

How can I write an unformated string to file, i.e., without interpreting % and any other special characters

3 ビュー (過去 30 日間)
oliver
oliver 2018 年 9 月 26 日
コメント済み: oliver 2018 年 9 月 26 日
I am trying to write strings (after some manipulation) from 1 file to another (line by line). These strings may contain things like "%" or other special characters. How can I write them to file and tell Matlab NOT to interpret them as placeholders or special characters but as plain text?
The obvious but awkward way would be to loop through all possible special chars and escape them one by one but I was hoping for a more elegant solution, e.g., a simple
fprint(fid,STRING,'interpreter','none')
that simply writes the string to file instead of an
fprintf(fid,STRING)
that looks for placeholders in STRING and tries to replace them. Thanks.

採用された回答

Kelly Kearney
Kelly Kearney 2018 年 9 月 26 日
You can pass the string as a parameter to a simple '%s' formatter, rather than using the string itself as a format:
>> fprintf('%s\n', 'Print this literally %s\n')
Print this literally %s\n

  1 件のコメント

oliver
oliver 2018 年 9 月 26 日
Excellent. Elegant and short. Thanks for the quick reply.

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

その他の回答 (0 件)

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by