How do I change the color of individual cells in uitable in MATLAB?

25 views (last 30 days)
I would like to change the color of individual cells in a uitable.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 May 2022
In R2019b and later version, for tables(uitable) created on uifigure or in container objects parented to uifigure (such as uipanel, uitab), you can use the uistyle / addStyle functions to specify the text color, background color, and other formatting options for certain cell in the table.
uit = uitable(uifigure, 'Data', randn(3));
[row,col] = find(uit.Data < 0); % get the index for negative values
s1 = uistyle;
s1.FontColor = 'red'; % set the style format
addStyle(uit,s1,'cell',[row,col]); % add style for the table
For details, please refer the documentation pages.

More Answers (2)

Rohail Razzaq
Rohail Razzaq on 15 Apr 2015
Edited: MathWorks Support Team on 1 Jul 2022
what if i have to use a particular variable instead of a specific text in the following line :
html<font color="blue">'my text'</font></html>
  1 Comment
Saurabh Harsh
Saurabh Harsh on 23 Apr 2015
Hi Rohail,
Please contact Mathworks technical support if you have follow up questions regarding the workaround.

Sign in to comment.


MathWorks Support Team
MathWorks Support Team on 3 Aug 2016
The ability to set the color of individual cells is not available through the UITABLE class methods. However, this is possible by the use of HTML elements by modifying their style properties.
The following code snippet demonstrates this technique:
 
function multi_colored_uitable
% Create a figure
f = figure;
% Define the column names
colnames = {'first', 'second', 'third', 'fourth','fifth','sixth'};
% Create a cell array with HTML code
celldata = {colText('Red text', 'red'), ...
colText('Blue text', 'blue'), ...
colText('Green text', 'green'), ...
colText('Purple text', 'purple'), ...
colText('RGB Green','rgb(0,255,0)'),...
colText('HEX Green','#00ff00')};
% Create a uitable with one row and 4 columns and colored cells
uitable(f, 'Data', celldata, ...
'ColumnName', colnames, ...
'Units', 'normalized', ...
'Position', [0 0 1 1]);
end
function outHtml = colText(inText, inColor)
% return a HTML string with colored font
outHtml = ['<html><font color="', ...
inColor, ...
'">', ...
inText, ...
'</font></html>'];
end

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!