Hi,
I would like to print my results from some previous calculations into a txt-file.
But apparently there is no linebreak in the output file for the string array and the results are not propperly displayed either
results = zeros(6,10);
i = transpose(num2str([1:size(results,2)].','%04d'));
fileID = fopen('exp.txt','wt');
fprintf(fileID,'%s %.2f %.2f %.2f %.2f %.2f %.2f\n', i, results);
fclose(fileID);
txt-file
0001000200030004000500060007000800090010 0.00 0.00 0.00 0.00 0.00 0.00
how i want it to look like:
0001 0.00 0.00 0.00 0.00 0.00 0.00
0002 0.00 0.00 0.00 0.00 0.00 0.00
...
Thank you for your help in advance!

 Réponse acceptée

dpb
dpb le 1 Déc 2018

1 vote

>> fmt=['%04d' repmat('%6.2f',1,size(results,2)) '\n'];
>> fprintf(fmt,[[1:size(results,1)].' results].')
0001 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0002 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0003 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0004 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0005 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0006 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
>>
You have to transpose the output array to display in row-major order--Matlab internal storage order is colum-major.

Plus de réponses (1)

Sebastian Netter
Sebastian Netter le 1 Déc 2018

0 votes

Thank you guys!
Apparently i can only accept one answer?
Both solutions worked :) Thanks again!

1 commentaire

dpb
dpb le 1 Déc 2018
Modifié(e) : dpb le 1 Déc 2018
Yes, the forum does only allow one Answer be accepted...
The two solutions are NOT identically the same, however, mine outputs the results array as it is defined--6 rows by 10 columns where as IA's outputs 10x6.
You can see the difference below if you define the results array so can see "who's who in the zoo"--
>> results=reshape(1:60,6,10);
results =
1 7 13 19 25 31 37 43 49 55
2 8 14 20 26 32 38 44 50 56
3 9 15 21 27 33 39 45 51 57
4 10 16 22 28 34 40 46 52 58
5 11 17 23 29 35 41 47 53 59
6 12 18 24 30 36 42 48 54 60
>>
% DPB solution -- as user interface shown...
>> fprintf(fmt,[[1:size(results,1)].' results].')
0001 1.00 7.00 13.00 19.00 25.00 31.00 37.00 43.00 49.00 55.00
0002 2.00 8.00 14.00 20.00 26.00 32.00 38.00 44.00 50.00 56.00
0003 3.00 9.00 15.00 21.00 27.00 33.00 39.00 45.00 51.00 57.00
0004 4.00 10.00 16.00 22.00 28.00 34.00 40.00 46.00 52.00 58.00
0005 5.00 11.00 17.00 23.00 29.00 35.00 41.00 47.00 53.00 59.00
0006 6.00 12.00 18.00 24.00 30.00 36.00 42.00 48.00 54.00 60.00
>>
% IA's solution -- internal memory order
>> fprintf('%04d %.2f %.2f %.2f %.2f %.2f %.2f\n', [1:size(results,2); results])
0001 1.00 2.00 3.00 4.00 5.00 6.00
0002 7.00 8.00 9.00 10.00 11.00 12.00
0003 13.00 14.00 15.00 16.00 17.00 18.00
0004 19.00 20.00 21.00 22.00 23.00 24.00
0005 25.00 26.00 27.00 28.00 29.00 30.00
0006 31.00 32.00 33.00 34.00 35.00 36.00
0007 37.00 38.00 39.00 40.00 41.00 42.00
0008 43.00 44.00 45.00 46.00 47.00 48.00
0009 49.00 50.00 51.00 52.00 53.00 54.00
0010 55.00 56.00 57.00 58.00 59.00 60.00
>>
Also note what happens with the implicit spacing of the format string when there are more significant digits in the number to be displayed.
So "works" is in the eyes of the beholder as to what is the desired result; we each made different assumptions in that regards.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by