Anyone knows how to use multiple format in fprintf?

5 vues (au cours des 30 derniers jours)
natt
natt le 2 Fév 2012
I normally assign the format to fprintf individually e.g.,
fprintf('%3.0f %3.0f %3.0f %3.0f',[10 10 10 10]);
But what if I would like to assign the format only once like:
fprintf('4*%3.0f',[10 10 10 10]);
(it does not work this way, just for an example)
Is there any way I can do this?
Thank you

Réponses (1)

Walter Roberson
Walter Roberson le 2 Fév 2012
No. However, you can use
ncol = 4;
fmt = [repmat('%3.0f ', 1, ncol - 1), '%3.0f\n'];
fprintf(fmt, [10 10 10 10]);
Or if you really do not want the newline:
fmt = repmat('%3.0f ', 1, ncol);
fmt(end) = []; %remove trailing space
fprintf(fmt, [10 10 10 10]);

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by