Create a conditional formatting Color Scale Table like Excel

조회 수: 49 (최근 30일)
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
Brendan Hamm
Brendan Hamm 2016년 4월 1일
편집: Brendan Hamm 2016년 4월 1일
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
Athanasios Petridis
Athanasios Petridis 2016년 4월 1일
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개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by