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

imateapot picture imateapot · Sep 14, 2011 · Viewed 15.4k times · Source

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?

Answer

Amro picture Amro · Sep 14, 2011

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)

screenshot