以另一种输出格式显示单元格阵列的内容 [英] Display cell array contents in another output format

查看:84
本文介绍了以另一种输出格式显示单元格阵列的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种显示单元格数组内容的方式,而不是使用celldisp来给我类似以下示例的输出:

I am searching for a way display cell array contents other than with celldisp that gives me an output like the example below:

celldisp(stuff)

和典型输出:

stuff{1} =
    10    70    20    50    50    90    90    30    30    60
stuff{2} =
    80    50    50    50    30    90    40    60    50    60    20    20
stuff{3} =
    20    90    10    80    20    30    30    70

我宁愿这样打印出来:

10    70    20    50    50    90    90    30    30    60
80    50    50    50    30    90    40    60    50    60    20    20
20    90    10    80    20    30    30    70

这可能吗?非常感谢您的帮助!

Is this possible? Help is much appreciated!

推荐答案

一种方法可能是使用 num2str ,如果未提供格式说明符,它将自动用空格填充条目.您可以通过循环 fprintf 调用来利用此行为.

One approach could be to use num2str, which automatically pads entries with a space if no format specifier is provided. You can take advantage of this behavior with a looped fprintf call.

例如:

stuff{1} = [10, 70, 20, 50, 50, 90, 90, 30, 30, 60];
stuff{2} = [80, 50, 50, 50, 30, 90, 40, 60, 50, 60, 20, 20];
stuff{3} = [20, 90, 10, 80, 20, 30, 30, 70];

for ii = 1:numel(stuff)
    fprintf('%s\n', num2str(stuff{ii}));
end

哪个会产生:

>> iheartSO
10  70  20  50  50  90  90  30  30  60
80  50  50  50  30  90  40  60  50  60  20  20
20  90  10  80  20  30  30  70

如果您愿意,也可以使用等效的 cellfun 调用想要所有的东西都紧凑:

You can also utilize the equivalent cellfun call if you want to be all compact and stuff:

cellfun(@(x)fprintf('%s\n', num2str(x)), stuff);

这篇关于以另一种输出格式显示单元格阵列的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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