How to change cell color based on data in a table (in Matlab)?

I have a matlab function that returns results in uitable.

The table has 2 columns and many rows: the first column is “values” and the second column is “security threshold / confidence interval”.

I would like to format the output so that certain cells are colored red: those for which the “value” in column 1 exceeds the corresponding “security threshold” in column 2.

Is there a way to do this using only Matlab?

PS: I know the following page:

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

but it seems to me that I have a lot of messing around, and I hope that since this post was made, perhaps Matlab caught up with and included this functionality?

+5
source share
1 answer

If you carefully read the discussion, you will 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

+8
source

All Articles