How do I flush right to content of my cell array in MATLAB (R2013a)?

2 views (last 30 days)
I have a cell array with mixed data types that I would like to have flushed right. That is, I want my cell entries to be right-aligned.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Oct 2013
Say you have a cell array defined as follows:
c = {'123';'Hello World!';'0.98765'};
Then , to have it right-aligned, you could create a function 'flushRight' defined by:
function output = flushRight(data)
% DATA is a cell array
[m,n] = size(data);
dataLengths = cellfun(@length,data,'UniformOutput',false);
maxLength = max(cell2mat(dataLengths));
output = cell(m,n);
for j = 1 : n
output(:,j) = cellfun(@(s) sprintf('%*s',maxLength(j),s),...
data(:,j),'UniformOutput',false);
end
end

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2012a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!