Create a conditional formatting Color Scale Table like Excel

Hello,
I am trying to create a Color Scale table similar to the one you can have from Excel. I would like the color of each cell to change based on the value in that cell compared to all values of the table or the values in that column.
For example, I have this table and I want to set the colour from red to green, where red is the smallest value in each column and green the highest:
x=[1,5,3;2,7,8;9,2,4]
x =
1 5 3
2 7 8
9 2 4
Using the sort function I can find the index of each element sorted in columns and in an ascending order.
[sX sInd]=sort(x);
sInd =
1 3 1
2 1 3
3 2 2
Now, I don't know how to use these data in order to create a uitable similar to this one:
Thanks in advance.

 채택된 답변

Brendan Hamm
Brendan Hamm 2016년 3월 31일
To do this you will need to pass in html as the Data for the uitable. The best way to replicate what you are looking for is to create an html table in each cell of the uitable.
>> x = [1,5,3;2,7,8;9,2,4];
>> [~,sInd] = sort(x);
% Create a color mapping
>> clrs = {'red','yellow','green'};
% Concatenate your html strings:
>> outHtml = strcat('<html><table border=0 width=400 bgcolor=', ...
clrs(sInd), ... % Choose the appropriate color for each cell
'"><TR><TD>', ...
cellfun(@num2str,num2cell(x),'UniformOutput',false), ... % Convert num data to cell of chars
'</TD></TR></body></html>');
% Place this in a uitable:
>> f = figure;
>> u = uitable(f,'Data',outHtml);

댓글 수: 3

Actually the sInd array has in ascending order the index(row number) of the smallest number to the biggest. Hence, the whole code you wrote doesn't give the correct colouring in the cells, for example in the second column). However, it's a good start. What it has to change is the sInd in the clrs(sInd). It has to be clrs(sIndNum), where sIndNum has in each cell the position of each value in the ascending order. I will try to find a way to do it.
I have managed to produce what I want by doing the following:
a(sInd(:,1))=clrs(:);
b(sInd(:,2))=clrs(:);
c(sInd(:,3))=clrs(:);
d=[a.',b.',c.'];
And then use d instead of clrs(sInd). Is there a better way, more efficient ?
Another improvement which it will be necessary is the widen the colour mix between red and green to more shades as I would like to work in more than 3 rows.
Sorry I missed that the ordering was wrong. The following will not necessary be more efficient, but will certainly generalize to more colors.
x = [1,5,3;2,7,8;9,2,4];
[~,sInd] = sort(x);
% Each row_i's values correspond to the row that should be
% colored with color_i for that column
% We can benefit then from linear indeing:
% Create a matrix the same size as x to represent the columns
cols = repmat(1:size(x,2),size(x,1),1);
% Get the linear indices
Ind = sub2ind(size(x),sInd,cols);
clrs = {'red';'yellow';'green'};
c = repmat(clrs,1,size(x,2));
A = cell(size(x));
A(Ind) = c;
A = reshape(A,size(x))
% Now use what Iposted above (replacing the second line)
outHtml = strcat('<html><table border=0 width=400 bgcolor=', ...
A, ... % Choose the appropriate color for each cell
'"><TR><TD>', ...
cellfun(@num2str,num2cell(x),'UniformOutput',false), ... % Convert num data to cell of chars
'</TD></TR></body></html>');
Now if you want more colors, i.e. have more columns, then likely you will want to change your colors from using strings to using hex color values. There are some posts on FileExchange for rgb2hex functions, so this requires you only define the rgb values you want. MATLAB has many builtin generators for colormaps . For instance the default colormap post 2014b is parula.
clrs = parula(4)
ans =
0.2081 0.1663 0.5292
0.0265 0.6137 0.8135
0.6473 0.7456 0.4188
0.9763 0.9831 0.0538
Each row is a different RGB color.
clrs = cellstr(rgb2hex(clrs)) % Now a cell which conforms with the html
It seems that works perfectly.
What I will try to do is create a colour bandwidth and each cell has a colour based on the weight of its value compared to the whole range of numbers in each column, similar to the Excel colour scale table, but it seems that it will take me a while to do that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by