How to modify uitable cell color according to data in table (in Matlab)?

微笑、不失礼 提交于 2019-12-17 18:45:57

问题


I have a matlab function that returns results in a uitable.

There are 2 columns and lots of rows to the table: first column is "values" and second column is a "safety threshold/confidence interval" of sorts.

I'd like to format the output so that certain cells get painted red: those for which the "value" in column 1 exceeds the corresponding "safety threshold" in column 2.

Is there a way to do this using just Matlab?

PS: I am aware of the following page:

http://www.mathworks.de/matlabcentral/newsreader/view_thread/150507

but it seems like a lot of tinkering to me, and I'm hoping that since that post was made, maybe Matlab has caught up and brought this functionality built in?


回答1:


If you read the discussion carefully, you'll find out that UITABLE supports HTML content...

Here is an example:

X = rand(100,2);

%# convert matrix of numbers to cell array of strings (right aligned)
XX = reshape(strtrim(cellstr(num2str(X(:)))), size(X));

%# find cells matching condition
idx = ( X(:,1) > X(:,2) );

%# use HTML to style these cells
XX(idx,1) = strcat(...
    '<html><span style="color: #FF0000; font-weight: bold;">', ...
    XX(idx,1), ...
    '</span></html>');

%# create table
f = figure;
h = uitable('Parent',f, 'Units','normalized', 'Position',[0.05 0.05 0.9 0.9]);

%# set table data
set(h, 'Data',XX)



来源:https://stackoverflow.com/questions/7409766/how-to-modify-uitable-cell-color-according-to-data-in-table-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!