how to change each cell color in a uitable

I wonder how can i make a uitable such that cells at this table has different background colorsand each column has different width
thank you

 Accepted Answer

Friedrich
Friedrich on 3 Jan 2012
Edited: Eric Sargent on 11 Feb 2023
Update:
Starting in R2019b you can use the uistyle, addStyle, and removeStyle functions.
f = uifigure;
uit = uitable(f, Data = "hi");
s = uistyle('BackgroundColor','red');
addStyle(uit,s,'column',1)
Original Answer
Hi,
this is pretty tricky within an uitable since the text you display will have a red background and not the full cell in the table:
uitable('Data',{'<body bgcolor="#FF0000">Hello</body>'})
You will see that Hello has a read background but thats all.
So we can do a small trick. Instead of adding the text we add a html table which contains the text. In addition this html table is sooooo wide that it needs the full cell^^:
uitable('Data',{'<table border=0 width=400 bgcolor=#FF0000><TR><TD>Hello</TD></TR> </table>' })
So overall you can do a colored table like this:
colergen = @(color,text) ['<table border=0 width=400 bgcolor=',color,'><TR><TD>',text,'</TD></TR> </table>'];
data = { 2.7183 , colergen('#FF0000','Red')
'dummy text' , colergen('#00FF00','Green')
3.1416 , colergen('#0000FF','Blue')
}
uitable('data',data)

14 Comments

This is such an improvement on the more common method of just giving a font a bgcolor. Thanks Friedrich!
Brilliant. I can't explain how much you've just helped me! The uitable still allows for resizable columns, and the background goes with it.
This answer does not work for me in MATLAB 2018b. It is just showing a table with the html as text.
In the colergen function, use '<html><table' instead of just '<table'
It's not working, any idia why?
Capturar.PNG
Works for me when I try. Which MATLAB release are you using?
I get the same results- html is not interpreted. It works when I use figure instead of uifigure:
table1 = uitable(figure);
table2 = uitable(uifigure);
When I do the above changes to table1 - the html is interpreted and I get colored cells, but not for table2. I am using Matlab 2018b
uifigure have a different underlying technology that does not support HTML like this. I have not researched them to see what is possible.
I have this example working only after adding an '<html>' tag to the above:
colergen = @(color,text) ['<html><table border=0 width=400 bgcolor=',color,'><TR><TD>',text,'</TD></TR> </table>'];
I need to change the color of each row in uitable(uifigure). If I'm using the html code, it is not interpreting the html as pointed out by Rafael and Simeon Georgiev. Can anyone suggest me how to tackle this issue?? I need a workaround for it.
For uitable that are inside uifigure, before R2019b there was only hacks.
As of R2019b, uitable inside uifigure can have styles configure for them. See https://www.mathworks.com/help/matlab/ref/matlab.ui.control.tableappd.addstyle.html
Thanks. Yeah, I read about it too. But the problem is, I'm currently on 2019a and I need the code to work with 2016 version onwards. So, I'm much more interested in the hacks. Could you please share?
Look on undocumented-matlab.com
Eric Sargent, it would appear that uistyle, addStyle, and removeStyle functions only work on uitable when the uitable is made in a uifigure, but not when a uitable is made in figure. That has me stuck at the moment.
Error using matlab.ui.control.Table/addStyle (line 63)
Functionality not supported with figures created with the figure function.

Sign in to comment.

More Answers (5)

Philip
Philip on 31 Mar 2017
In Matlab 2017a (and at least as far back as 2013b) this has improved. The uitable now has a "BackgroundColor" property. This is an array of n rows and 3 columns, RGB values between 0 and 1. It appears that if this array has less rows than the data in the table, then the colours are repeated throughout the table.
To highlight a single row, create a colour array the same size as your data and specify the row of interest as the colour you want.
This is how the alternating row colours are created (and so those colours can be changed if you want as well).

1 Comment

Hello, that is my way to try to get the GUI uitable BAckgroundColor changed. But it is only changing the whole uitable. I want it to just change the row of the checkbox green if the checkbox is 1 and if checkbox is 0, change to red background of the row. Thanks for your help. best regards

 function pushbutton2_Callback(hObject, eventdata, handles)
 global pushbutton
 [num,txt,raw]= xlsread(uigetfile ({'.xlsx'}))
 anzahl_kriterien = size([raw],1)% --> aendern
 kriterium =  cell(anzahl_kriterien);
 tabledata = [num2cell(true(length(raw),1)),raw];
 set(handles.uitable3, 'data',tabledata)
 setappdata(handles.uitable3,'RawTableDat',raw)
 % --- Executes when entered data in editable cell(s) in uitable3.
 function uitable3_CellEditCallback(hObject, eventdata, handles)
 tabledata=get(handles.uitable3,'Data');
 spalte1=tabledata(:,1) 
 spalte2=tabledata(:,2)
 zeilenanzahl=length(tabledata); 
 % uitablehandles=findobj(handles.uitable3);
  for j = 1:1:zeilenanzahl %für zeile 1 bis ende
    if (spalte1{j,1}==1)   
       tabledata{j,2}=spalte2{j,1}
       set(handles.uitable3,'BackgroundColor',[0 1 0]) 
    end
for j = 1:1:zeilenanzahl  
    if (tabledata{j,1}==0) 
 tabledata{j,2}= set(handles.uitable3,'BackgroundColor',[1 0 0])
 end
  end 
 end
 guidata(hObject,handles);
 % --- Executes during object creation, after setting all    properties.
  function uitable3_CreateFcn(hObject, eventdata, handles)
  hObject.ColumnFormat = {'logical',[]}; 
  hObject.ColumnEditable = logical([1 0]); 

Sign in to comment.

4 Comments

thank you Walter
your answer is of great help. But what if i want to change the background colors not the foreground colors
Regards
Eldally
You could try
'<HTML><TD BGCOLOR=#40FF68>Hello</TD>'
Sorry this is the first time to use html. when i used the statment in the code you gave me before it gives no colr difference. What I want is to form a table say 5rx6c and the last column to be with five background colors
regards
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>

Sign in to comment.

Hello, that is my way to try to get the GUI uitable BAckgroundColor changed. But it is only changing the whole uitable. I want it to just change the row of the checkbox green if the checkbox is 1 and if checkbox is 0, change to red background of the row. Thanks for your help. best regards
function pushbutton2_Callback(hObject, eventdata, handles)
global pushbutton
[num,txt,raw]= xlsread(uigetfile ({'.xlsx'}))
anzahl_kriterien = size([raw],1)% --> aendern
kriterium = cell(anzahl_kriterien);
tabledata = [num2cell(true(length(raw),1)),raw];%das nur einmal checkbox spalte da ist und am anfang
set(handles.uitable3, 'data',tabledata)
setappdata(handles.uitable3,'RawTableDat',raw)
% --- Executes when entered data in editable cell(s) in uitable3.
function uitable3_CellEditCallback(hObject, eventdata, handles)
tabledata=get(handles.uitable3,'Data');
spalte1=tabledata(:,1)
spalte2=tabledata(:,2)
zeilenanzahl=length(tabledata);
for j = 1:1:zeilenanzahl %für zeile 1 bis ende
if (spalte1{j,1}==1)
tabledata{j,2}=spalte2{j,1}
set(handles.uitable3,'BackgroundColor',[0 1 0])
end
for j = 1:1:zeilenanzahl
if (tabledata{j,1}==0)
tabledata{j,2}= set(handles.uitable3,'BackgroundColor',[1 0 0])
end
end
end
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function uitable3_CreateFcn(hObject, eventdata, handles)
hObject.ColumnFormat = {'logical',[]};
hObject.ColumnEditable = logical([1 0]);

1 Comment

Hi there...
Did you find any solution to this problem? I'm also looking for the same.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!