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
0 个评论
回答(1 个)
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]);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!