fprintf varaibles to a text file does not add cr/lf so text wraps

21 views (last 30 days)
I'm writing varaibles to a text file but the written text wraps. Any idea what I'm doing wrong.
%format output
pl1 = sprintf(' Gen = %d ', GenNo);
pl2 = sprintf(' Mem = %d ', GenMemNo);
pl3 = sprintf(' Try = %d ', TryNo);
pl4 = sprintf(' Samp = %d ', SampNo);
pl5 = sprintf(' Path = ');
pl6 = sprintf('%d ', OriginalPositions);
pl7 = sprintf(' Cost = %f ', cost);
pl8 = sprintf(' Var = ');
pl9 = sprintf('%f ', inputvar);
pl10 = sprintf(' Skew = ');
pl11 = sprintf('%f ', inputskew);
pl12 = sprintf(' Kurtosis = ');
pl13 = sprintf('%f ', inputkurtosis);
pl14 = sprintf(' Volts = ');
pl15 = sprintf('%f ', volts);
pl16 = sprintf(' AvInputs = ');
pl17 = sprintf('%f ', AvInputs);
pl18 = sprintf('\n ');
%make a single string
PrintLine = strcat(pl1,pl2,pl3,pl4,pl5,pl6,pl7,pl8,pl9,pl10,pl11,pl12,pl13,pl14,pl15,pl16,pl17,pl18);
%write to screen & Log.txt
%fprintf(PrintLine,'\r ');
fprintf(fileID,PrintLine,'\r ');
thanks

Accepted Answer

Geoff Hayes
Geoff Hayes on 24 Nov 2014
Robert - if you want a line break for each "assignment", then you will need to add it after each numeric input. Try adding \n after each of your "values" as
pl1 = sprintf(' Gen = %d\n', GenNo);
pl2 = sprintf(' Mem = %d\n', GenMemNo);
pl3 = sprintf(' Try = %d\n', TryNo);
pl4 = sprintf(' Samp = %d\n', SampNo);
pl5 = sprintf(' Path = ');
pl6 = sprintf('%d\n', OriginalPositions);
pl7 = sprintf(' Cost = %f\n', cost);
pl8 = sprintf(' Var = ');
pl9 = sprintf('%f\n', inputvar);
pl10 = sprintf(' Skew = ');
pl11 = sprintf('%f\n', inputskew);
pl12 = sprintf(' Kurtosis = ');
pl13 = sprintf('%f\n', inputkurtosis);
pl14 = sprintf(' Volts = ');
pl15 = sprintf('%f\n', volts);
pl16 = sprintf(' AvInputs = ');
pl17 = sprintf('%f\n', AvInputs);
pl18 = sprintf('\n ');
The above, once written to file, should have the appropriate line breaks (carriage returns).

More Answers (1)

Image Analyst
Image Analyst on 24 Nov 2014
Whether it wraps or not depends on the application. In some it will be wrapped, in other not. If it's not wrapped in the application you want, then use both \n and \r:
fprintf('blah blah blah\r\n');
If that doesn't work, reverse them:
fprintf('blah blah blah\n\r');
I'm pretty sure one of those will work. You can do it with the sprintf's too.
  2 Comments
Image Analyst
Image Analyst on 25 Nov 2014
ROBERT's "Answer" moved here:
Tried adding the \r\n & \n\r to both the sprintf & the fprintfs to no avail. I still get :
Gen = 1 Mem = 1 Try = 1 Samp = 1 Path =6 5 7 8 11 12 1 10 4 2 3 9 Cost = 12.830831 Var =0.125679 Skew =1.927805 Kurtosis =5.842719 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.074864 -0.505246 -0.429361 -0.654585 -2.885561 -2.891856 -2.817342 -2.655005 -0.000287 -1.870880 -2.093887 -2.082704 Gen = 1 Mem = 1 Try = 1 Samp = 2 Path =6 5 7 8 11 12 1 10 4 2 3 9 Cost = 12.830831 Var =0.122407 Skew =1.859722 Kurtosis =5.583300 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.069018 -0.513226 -0.437441 -0.668212 -2.886154 -2.892222 -2.819631 -2.655355 -0.000300 -1.859581 -2.082851 -2.072078 Gen = 1 Mem = 1 Try = 1 Samp = 3 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.122117 Skew =1.863252 Kurtosis =5.610241 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.073944 -0.506777 -0.429746 -0.666589 -2.885753 -2.892955 -2.820666 -2.651704 -0.000344 -1.858075 -2.079878 -2.070643 Gen = 1 Mem = 1 Try = 1 Samp = 4 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.125912 Skew =1.933436 Kurtosis =5.886012 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.086335 -0.495429 -0.417896 -0.651529 -2.885298 -2.893447 -2.819764 -2.651187 -0.000319 -1.870725 -2.090955 -2.082096 Gen = 1 Mem = 1 Try = 1 Samp = 5 Path =6 5 7 8 11 12 1 10 4 2 3 9 Cost = 12.830831 Var =0.128896 Skew =1.978310 Kurtosis =6.053309 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.090340 -0.495208 -0.417901 -0.643542 -2.885812 -2.893363 -2.818628 -2.655186 -0.000306 -1.882034 -2.103547 -2.092497 Gen = 1 Mem = 1 Try = 1 Samp = 6 Path =6 5 7 8 11 12 1 10 4 2 3 9 Cost = 12.830831 Var =0.127572 Skew =1.947805 Kurtosis =5.921965 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.084299 -0.503683 -0.427628 -0.651939 -2.886725 -2.893109 -2.819652 -2.659744 -0.000291 -1.879349 -2.101667 -2.089405 Gen = 1 Mem = 1 Try = 1 Samp = 7 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.124288 Skew =1.883142 Kurtosis =5.677322 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.079598 -0.509872 -0.433794 -0.664712 -2.887081 -2.893456 -2.821826 -2.659156 -0.000299 -1.867963 -2.089935 -2.078501 Gen = 1 Mem = 1 Try = 1 Samp = 8 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.124477 Skew =1.902538 Kurtosis =5.761130 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.085827 -0.501954 -0.424594 -0.660707 -2.886591 -2.894173 -2.822487 -2.655404 -0.000305 -1.868611 -2.089319 -2.079054 Gen = 1 Mem = 1 Try = 1 Samp = 9 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.128772 Skew =1.976788 Kurtosis =6.052106 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.097885 -0.490726 -0.413063 -0.645020 -2.886190 -2.894476 -2.821294 -2.655558 -0.000301 -1.882999 -2.102042 -2.091621 Gen = 1 Mem = 1 Try = 2 Samp = 1 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.118132 Skew =1.861085 Kurtosis =5.592739 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.064633 -0.506445 -0.430301 -0.686706 -2.884754 -2.890803 -2.817506 -2.650732 -0.000319 -1.861549 -2.082907 -2.053927 Gen = 1 Mem = 1 Try = 2 Samp = 2 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.117802 Skew =1.871255 Kurtosis =5.642395 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.068355 -0.499481 -0.422050 -0.683368 -2.883986 -2.891277 -2.817540 -2.645650 -0.000309 -1.859179 -2.079738 -2.052672 Gen = 1 Mem = 1 Try = 2 Samp = 3 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.121213 Skew =1.936212 Kurtosis =5.897018 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.077100 -0.490062 -0.412131 -0.668973 -2.883469 -2.891370 -2.815971 -2.644643 -0.000318 -1.869993 -2.089538 -2.063039 Gen = 1 Mem = 1 Try = 2 Samp = 4 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.122923 Skew =1.954787 Kurtosis =5.964021 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.076495 -0.493589 -0.416228 -0.664813 -2.883884 -2.891045 -2.814631 -2.648078 -0.000326 -1.875674 -2.097193 -2.069335 Gen = 1 Mem = 1 Try = 2 Samp = 5 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.120660 Skew =1.901067 Kurtosis =5.749147 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.066920 -0.504112 -0.427591 -0.675735 -2.884627 -2.890619 -2.815393 -2.651031 -0.000326 -1.867883 -2.090760 -2.062800 Gen = 1 Mem = 1 Try = 2 Samp = 6 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.116742 Skew =1.828448 Kurtosis =5.467191 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.059429 -0.511513 -0.434980 -0.689405 -2.884748 -2.890842 -2.817288 -2.649152 -0.000288 -1.852797 -2.076010 -2.049735 Gen = 1 Mem = 1 Try = 2 Samp = 7 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.116314 Skew =1.835491 Kurtosis =5.505054 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.062835 -0.504848 -0.427191 -0.686616 -2.884114 -2.891405 -2.817511 -2.644249 -0.000286 -1.850068 -2.072419 -2.048209 Gen = 1 Mem = 1 Try = 2 Samp = 8 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.119615 Skew =1.900254 Kurtosis =5.758772 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.071745 -0.495302 -0.417157 -0.672276 -2.883580 -2.891570 -2.816103 -2.643099 -0.000299 -1.860681 -2.081905 -2.058449 Gen = 1 Mem = 1 Try = 2 Samp = 9 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.121521 Skew =1.922803 Kurtosis =5.841141 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.071587 -0.498409 -0.420686 -0.667100 -2.884031 -2.891339 -2.814764 -2.646447 -0.000308 -1.866892 -2.090132 -2.065565 Gen = 1 Mem = 1 Try = 3 Samp = 1 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.134510 Skew =2.050388 Kurtosis =6.326584 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.114234 -0.495065 -0.416161 -0.639192 -2.889866 -2.896470 -2.824716 -2.672348 -0.000319 -1.911887 -2.133588 -2.113806 Gen = 1 Mem = 1 Try = 3 Samp = 2 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.130559 Skew =1.995279 Kurtosis =6.109048 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.109979 -0.499884 -0.421112 -0.652694 -2.890092 -2.896482 -2.826233 -2.671432 -0.000335 -1.900439 -2.121273 -2.102175 Gen = 1 Mem = 1 Try = 3 Samp = 3 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.129469 Skew =1.996738 Kurtosis =6.123589 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.112300 -0.493957 -0.413986 -0.653191 -2.889571 -2.896727 -2.827454 -2.667066 -0.000326 -1.897317 -2.117472 -2.098935 Gen = 1 Mem = 1 Try = 3 Samp = 4 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.131405 Skew =2.035761 Kurtosis =6.279902 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.119953 -0.485890 -0.405305 -0.644261 -2.888800 -2.897180 -2.826780 -2.664762 -0.000326 -1.902839 -2.122063 -2.103346 Gen = 1 Mem = 1 Try = 3 Samp = 5 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.135569 Skew =2.099056 Kurtosis =6.527979 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.130280 -0.475830 -0.395453 -0.631078 -2.888408 -2.897184 -2.824789 -2.665443 -0.000328 -1.916547 -2.133974 -2.114605 Gen = 1 Mem = 1 Try = 3 Samp = 6 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.138310 Skew =2.134613 Kurtosis =6.665399 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.131986 -0.475405 -0.394981 -0.624589 -2.888778 -2.896774 -2.823830 -2.668543 -0.000346 -1.926502 -2.145516 -2.123664 Gen = 1 Mem = 1 Try = 3 Samp = 7 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.136937 Skew =2.101484 Kurtosis =6.527657 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.124435 -0.485937 -0.406560 -0.632719 -2.889513 -2.896385 -2.824334 -2.673072 -0.000357 -1.923480 -2.143812 -2.120011 Gen = 1 Mem = 1 Try = 3 Samp = 8 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.132567 Skew =2.036959 Kurtosis =6.269659 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.117435 -0.495172 -0.416302 -0.648448 -2.889997 -2.896371 -2.825618 -2.673838 -0.000358 -1.911120 -2.131667 -2.107626 Gen = 1 Mem = 1 Try = 3 Samp = 9 Path =6 5 7 8 11 1 12 10 4 2 3 9 Cost = 12.898979 Var =0.130210 Skew =2.016900 Kurtosis =6.197556 Volts =-3.000000 -1.875000 -0.750000 2.999908 AvInputs =-2.117064 -0.491925 -0
Image Analyst
Image Analyst on 25 Nov 2014
Try a simple case, like where you're just writing out 3 lines with 3 variables. Attach your code and output file so we can check it. Something like
fid = fopen('test.txt', 'wt');
fprintf(fid, 'This is line %d\n', 1);
fprintf(fid, 'This is line %d\n', 2);
fprintf(fid, 'This is line %d\n', 3);
fclose(fid);
msgbox('Done creating file');
For me, when I double click on the file and it opens in Notepad, the lines are on separate lines, so this works - no \r needed.
ARE YOU SURE you opened the file with 'wt' in the fopen() statement and not the 'w' option?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!