Anyone knows how to use multiple format in fprintf?

5 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2012-2-2
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]);

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by