vertically aligned data using fprintf

27 vues (au cours des 30 derniers jours)
sermet OGUTCU
sermet OGUTCU le 11 Nov 2021
Modifié(e) : Jan le 11 Nov 2021
data=
1 0 -0.828330184322257 -1.90936748180674 -0.973747768469492
1 300 -0.838221643420248 -1.73368790711073 -0.694053671027946
1 600 -0.719016934251202 -1.77992572323036 -0.809246004716909
1 900 -0.685322042436873 -1.62664527333989 -0.659059040624753
1 1200 -0.598472883146815 -1.68630603450331 -0.631032590099370
The above data matrix is just a part of my orginal data file. I use the following code to print the data in a text file:
fprintf(fid, '\n');
fprintf(fid,'%02d %d %.3f %.3f %.3f\n', data');
fclose(fid);
The printed results in a text file looks like:
01 0 -0.828 -1.909 -0.974
01 300 -0.838 -1.734 -0.694
01 600 -0.719 -1.780 -0.809
01 900 -0.685 -1.627 -0.659
01 1200 -0.598 -1.686 -0.631
As seen, the vertical alignment of the columns (except for 1st column) are not maintained due to the different size of 2nd column and the signs (+,-) of 3-4-5 columns. How I can print the vertically aligned data matrix of each column?

Réponse acceptée

Jan
Jan le 11 Nov 2021
Modifié(e) : Jan le 11 Nov 2021
Use the width in the format specifier
data = [1 0 -0.828 -1.909 -0.974; ...
1 300 -0.838 -1.734 -0.694; ...
1 600 -0.719 -1.780 -0.809; ...
1 900 -0.685 -1.627 -0.659; ...
1 1200 -0.598 -1.686 -0.631];
fprintf('%02d %-8d%16.3f%16.3f%16.3f\n', data');
01 0 -0.828 -1.909 -0.974 01 300 -0.838 -1.734 -0.694 01 600 -0.719 -1.780 -0.809 01 900 -0.685 -1.627 -0.659 01 1200 -0.598 -1.686 -0.631

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by