Extra commas when using fprintf
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Oscar Tsang
le 6 Déc 2019
Réponse apportée : Guillaume
le 7 Déc 2019
Well, my problem is that within the for loop im printing the number of iterartions and the old means using fprintf. The only problem is that it adds a extra comma to the end of the old means when its not needed. I have treid to google the solution, still can't find an answer. Below is the code when i print the iterations and the old means.
I have also attached a picture of what it looks like.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253354/image.jpeg)
for i =1:I
% print out iterations
fprintf("\n Iterations: %d \n",i );
fprintf(" Old Means: ");
fprintf("(");
fprintf("%.4f," ,M(:,:,i));
fprintf(")");
% fprintf("\n Clusters: (%d,%d,%d,%d,%d,%d,%d,%d,)" ,C());
% fprintf("\n New means: \n")
%
% fprintf(" Old Means: %.4f,%.4f,%.4f,%.4f,%.4f \n ",M(:,:,:));
end
0 commentaires
Réponse acceptée
Guillaume
le 7 Déc 2019
Personally, I'd use:
fprintf(" Old Means: (%s)", strjoin(compose('%.4f', M(:, :, i)), ','));
0 commentaires
Plus de réponses (1)
Walter Roberson
le 7 Déc 2019
Replace
fprintf("%.4f," ,M(:,:,i));
With
Temp = M(:, :, i) ;
fprintf("%.4f," ,Temp(1:end-1) );
fprintf("%.4f", Temp(end)) ;
There are other approaches, such as computing the format according to the data size with repmat, and zapping the final comma before using the format.
0 commentaires
Voir également
Catégories
En savoir plus sur Whos dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!