Display cell array contents in another output format

天涯浪子 提交于 2019-12-11 03:42:41

问题


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)

and a typical output:

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

I would prefer to print it out like this instead:

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!


回答1:


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.

For example:

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

Which produces:

>> 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

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);


来源:https://stackoverflow.com/questions/39479920/display-cell-array-contents-in-another-output-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!