How do I print a % character into a file

I am creating an app in App Designer, creating an output file that will be an .m file for another user to run later.
I'd like to put a comment header at the start of this file but I can't double excape the % character
app.SessionFile = fopen(app.SessionFileName,'at');
str = sprintf("%% Parameters: %c%u...",v1,v2...); % Put required string into str - works
str is now "% Paramters: ...." but it won't go out to the file with
fprintf(app.SessionFile,str); % Nothing gets printed, comment gets stripped out!
(0 chars printed)
Also not working
str = sprintf("%%% Parameters:
str = sprintf(" %% Parameters:
Seems like if I have a string I want to put in a text file MatLab shouldn't mess with it.
Also: is 'at' the same as 'wt' ? seems like it always appends anyway with fprint.

3 Comments

"Seems like if I have a string I want to put in a text file MatLab shouldn't mess with it."
Aaah, so it is all MATLAB's fault.
Lets have a look at the information you have given us:
'str is now "% Paramters: ...."'
'fprintf(app.SessionFile,str);'
That is all we need to know. You are providing one format string to FPRINTF (with no optional arrays), it contains the special character '%' at the start all by itself, without being part of any formatting operator:
That is of course syntactically meaningless: either this special character must be part of a formatting operator or it must be escaped, just as the FPRINTF documentation explains. All such special characters must be escaped, if they are to appear literally. Your format string uses a special character without escaping it nor as part of a formatting operator.
Question: what do you expect FPRINTF to do, when a user provides an invalid format string?
fprintf('%') % invalid format string -> prints nothing
OK ''wt' is same as 'at' on first open otherwise a appends w restarts... Stupid question!
is printing direct to the filw with fprinf(MyFile,"%% ") the only way to do it?
Thus second fprintf(MyFile,str); is needed to add the rest of the string.
fprintf(MyFile, "%s", "%")
or
fwrite(MyFile, "%s")
Again I caution that neither version of those will add a newline.

Sign in to comment.

 Accepted Answer

fprintf(app.SessionFile,"%s",str);

1 Comment

Or skip using sprintf if you don't need str for anything else and fprintf v1, v2, etc., directly:
fprintf(app.SessionFile,"%% Parameters: %c%u...",v1,v2...);

Sign in to comment.

More Answers (2)

If you don't need str inside your code other than to write it to the file, rather than creating it just write directly to the file.
app.SessionFile = fopen(app.SessionFileName,'at');
fprintf(app.SessionFile,"%% Parameters: %c%u...",v1,v2...);
Either
fprintf(app.SessionFile,"%s",str);
or
fwrite(app.SessionFile, str);
Note: your str does not appear to include newline. Neither of the above possibilities will insert a newline. The easiest way to include a newline is
fprintf(app.SessionFile,"%s\n",str);

4 Comments

The last approach would be likely the best:
fprintf(app.SessionFile,"%s\n",str);
This allows STR to be produced literally, making code writing and debugging easier.
I understand about \n new lines and use them when I need them. That wasn't the problem.
I did have \n at the end of str as built IRL, sometimes I want to add more stuff to the end of the same line.
Not sure how/why it helps when you fprintf(MyFile,"%s",str); vs fprintf(MyFile,str); is that documented anywhere?
Also if I write an unterminated line to a file and then close it will it not include that line in the file? Seems odd, a text file doesn't need to end with an EOL and in WIndows often doesn't though it is better if it's there.
fprintf(MyFile,str) treats str as the format, an interprets it, scanning it looking for % formats.
fprintf(MyFile, "%s", str) treats %s as the format, and treats str as the text to be inserted literally.
There is no special documentation for fprintf(MyFile,str) because it is just treated as an instance of fprintf(fileID,formatSpec,A1,...,An) with str as the formatSpec and with no A1,...,An
If you write an unterminated line to a file and close the file, then the line will be inserted into the file, and the file will just end there; the line can be pulled out of the file normally. It is possible some common DOS utilities might not show the trailing line.
Great answer but I chose the simplest one that works.

Sign in to comment.

Categories

Products

Release

R2023b

Asked:

on 13 Sep 2024

Commented:

on 23 Sep 2024

Community Treasure Hunt

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

Start Hunting!