EBH
EBH

Reputation: 10440

Ignore the comment sign (%) in m-file within a string

In my code I have the following line:

fprintf(logfile,'Parameters: Size: %d\tH: %.4f\tF: %.1f\tI: %.3f\tR: %d\tSigma: %d\tDisp: %.1f\r\n',parameter_sets(ps,:));

which is too long, so I want to break it to:

fprintf(logfile,'Parameters: Size: %d\tH: %.4f\tF: %.1f\tI: %.3f\tR: ...
        %d\tSigma: %d\tDisp: %.1f\r\n',parameter_sets(ps,:));

However, since the brake is within a string, MATLAB see the formatting %d sign in the second line as a start of a comment, and ignore this line (and produce an error...).

So I tried to make it clearer with a [] that warp the string:

fprintf(logfile,['Parameters: Size: %d\tH: %.4f\tF: %.1f\tI: %.3f\tR: ...
        %d\tSigma: %d\tDisp: %.1f\r\n'],parameter_sets(ps,:));

but no help, it still interpret the second line as a comment. I also tried with and without the ellipsis (...) in different places, with no success.

So how can I write a line in a formatted way (i.e. a reasonable length) if it has a % sign in it?

Upvotes: 1

Views: 52

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

Divide it in two lines like this:

fprintf(logfile,['Parameters: Size: %d\tH: %.4f\tF: %.1f\tI: %.3f\tR:', ...
        '%d\tSigma: %d\tDisp: %.1f\r\n'],parameter_sets(ps,:));

% notice the apostrophe and comma(',) before ellpsis(...) at the end of first line
% and apostrophe(') at the start of the second line

Upvotes: 3

Related Questions