How can I manually set the horizontal alignment of cells in UITABLE in MATLAB?

10 views (last 30 days)
I would like to be able to set  the column horizontal alignment of cells {'left','center','right'}.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 May 2022
If you use R2019b or later versions, for UIFigure (or App Designer), you can use uistyle function.
fig = uifigure;
uit = uitable(fig);
uit.Data = rand(5,3);
s = uistyle('HorizontalAlignment','center'); % create a style
addStyle(uit,s) % adjust the style
For details, please see the following documentation.
・MATLAB: uistyle 
If you use R2019a or previous releases, please refer the following information.
The ability to manually set the horizontal alignment in UITABLE cells is not available in MATLAB.
As an alternative to the display position of the data, please consider converting the numerical value to a character string and changing it to right alignment, or adjusting the display position using space.
fig = figure;
uit = uitable(fig);
uit.Data = rand(5,3);
data = num2cell(uit.Data); % convert to cell array
data2 = cellfun(@num2str,data,'UniformOutput',false); % convert to characters
uit.Data = data2; % Left alignement
% Insert spaces
data2 = cellfun(@(s)sprintf('%*s',12,s),data2,'UniformOutput',false);
uit.Data = data2; % like center alignment

More Answers (0)

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!