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