Create a conditional formatting Color Scale Table like Excel
32 ビュー (過去 30 日間)
古いコメントを表示
Athanasios Petridis
2016 年 3 月 31 日
コメント済み: Athanasios Petridis
2016 年 4 月 1 日
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.
0 件のコメント
採用された回答
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
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!