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

15,633

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

Share:
15,633
imateapot
Author by

imateapot

Updated on June 13, 2022

Comments

  • imateapot
    imateapot almost 2 years

    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?

  • Yair Altman
    Yair Altman over 12 years
    in fact, there is no need to close the HTML tags (</span></html>)
  • Yair Altman
    Yair Altman almost 11 years
    For anyone interested, I posted a dedicated (expanded) article about this issue on my website: UndocumentedMatlab.com/blog/uitable-cell-colors
  • Amro
    Amro almost 11 years
    definitely worth reading, thanks again Yair. Perhaps you can post an answer here with a small example showing the "cell renderer" method. You will certainly get my vote :)
  • Yair Altman
    Yair Altman almost 11 years
    Amro - thanks for the kind words. I did post an answer with this link back in 2011 but it was deleted for being "self-promotional" and "nothing but a click-through link". When I asked about it I got tons of cold water poured over my head, so it kind of blew the wind from my sails, so to speak...
  • Amro
    Amro almost 11 years
    @YairAltman: sorry you had a bad experience. Your article is definitely on topic and solves the issue at hand. But I guess it would have been more agreeable with the ways of Stack Overflow if you had included a summary in addition to the link, that way the answer is self-contained (you could makes the changes and vote to undelete if you still want to)... Anyway, let me just say that your contributions in MATLAB community are always appreciated
  • Admin
    Admin almost 11 years
    @Amro: Using this XX(idx,1) = strcat(... '<html><span style="color: #FF0000; font-weight: bold;">', ... XX(idx,1), ... '</span></html>'); in my code produces some kind of weird symbols. My data XX is a cell array..
  • Amro
    Amro almost 11 years
    @Pupil: I have no way of knowing what your code is doing, but I suspect that your cell array contains numeric data not strings (i.e 1 not '1'). If that's not it, please create a new question and post the needed info...
  • Admin
    Admin almost 11 years
    @Amro: I have created a new question based on above query. Thanks!
  • Amro
    Amro almost 11 years
    I posted an answer there. As I suspected, you have numeric values instead of all strings. That's the reason why I used NUM2STR in the example shown above