Extra commas when using fprintf
조회 수: 16 (최근 30일)
이전 댓글 표시
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.
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
채택된 답변
Guillaume
2019년 12월 7일
Personally, I'd use:
fprintf(" Old Means: (%s)", strjoin(compose('%.4f', M(:, :, i)), ','));
댓글 수: 0
추가 답변 (1개)
Walter Roberson
2019년 12월 7일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Software Development Tools에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!