How to format the length of a line of text using fprintf in a loop
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am using MATLAB R2018a trying to print a text file using the following script which uses fprintf in a for loop. It prints the text file data corretly however I need the text to be right justified as can be seen in lines 1-13 of the second picture, however, lines 14-24 are indented to the left due to the length of "timestr" changing. I have tried adding '%42f' before X in my loop to set the line length to 42 characters but it did not work. Any help would be appreciated.
2 commentaires
Réponses (1)
Jos (10584)
le 7 Fév 2019
Why don't you format the values within each string to be nicely aligned, before sending them to a text file?
A = rand(4,3) ;
for k=1:size(A,1)
str = sprintf('%8.3f', A(k,:)) ;
fprintf('%s\n', str)
end
0 commentaires
Voir également
Catégories
En savoir plus sur String Parsing 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!