Matlab输出-空间填充? [英] Matlab output - Space padding?

查看:150
本文介绍了Matlab输出-空间填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试输出一个矩阵:

I'm trying to output a Matrix:

M = [1 20 3; 22 3 24; 100 150 2];

使用:

for i=1:3
    fprintf('%f\t%f\t%f\n', M(i), M(i+length(M)), M(i+length(M)*2));
end

输出结果如下:

1 20 3
22  3  24
100  150  2

显然不是很好.我如何获得它,以便在整数的前面填充空格?像这样:

Which is obviously not great. How can I get it so the front of integers are padded with spaces? Like so:

  1   20   3
 22    3  24
100  150   2

有什么想法吗?

谢谢!

推荐答案

您可以使用

You can use string formatting to allocate specific number of characters per displayed number.
For example

 fprintf('% 5d\n', 12) 

用5个字符打印12,并用空格填充未使用的3个前导字符.

prints 12 in 5 characters, padding the un-used 3 leading characters with spaces.

这篇关于Matlab输出-空间填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆