4

I am trying to create an array of numbers (converted to string) that fall bellow a thresh hold, for my current testing i'm using 0.5. I need the font of every value of my table that falls above or below my thresh hold to be colored red, in my current code i'm only using 2 columns, but I will be using more than 10. This is my code right now and it is only displaying the numbers values above 0.5 in color red but it's not displaying the numbers below 0.5 (they should be in black). I'm sorry for the bad naming of the variables, I'm just testing to implement this. Help will be greatly appreciated.

TTT = rand(30,2);
for u = 1:2

PPP = TTT(1:30, u:u);

   RRR = ( PPP(:) > .5);

   AAA = reshape(strtrim(cellstr(num2str(TTT(:)))), size(TTT));

   QQQQ(RRR, u) = strcat(...
   '<html><span style="color: #FF0000; font-weight: bold;">', ...
    AAA(RRR, u), ...
   '</span></html>');


end
%# 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',QQQQ) 
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
barracuda
  • 968
  • 3
  • 10
  • 26

1 Answers1

3

You are not filling all values in QQQQ, only those which will be in red. The rest (which should be in black) are left as empty cells, and thus they are not shown.

To correct this, You need to initialize QQQQ to AAA, and then modify color for the relevant cells. So, add

AAA = reshape(strtrim(cellstr(num2str(TTT(:)))), size(TTT));
QQQQ = AAA;

right before the for loop, and remove the AAA = reshape... line from within the loop. That is:

TTT = rand(30,2);
AAA = reshape(strtrim(cellstr(num2str(TTT(:)))), size(TTT));
QQQQ = AAA;
for u = 1:2
   PPP = TTT(1:30, u:u);
   RRR = ( PPP(:) > .5);
   QQQQ(RRR, u) = strcat(...
      '<html><span style="color: #FF0000; font-weight: bold;">', ...
      AAA(RRR, u), ...
      '</span></html>');
end

%# 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',QQQQ) 
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Thanks allot!!! that really solved the first half of my problems, I also need the fonts of all values in column 1 to also be colored red if they are above 0.50. – barracuda Dec 14 '13 at 00:01
  • @Stephan Sorry. The initiallizaton has to be done before the `for` loop. See updated answer – Luis Mendo Dec 14 '13 at 00:06
  • Wow, thanks allot Luis it worked perfectly. I never notice those errors, thanks again, I spent allot of hours figuring this out. – barracuda Dec 14 '13 at 00:19
  • @Stephan Glad I could help. As you probably know, you should consider upvoting and/or marking the answer as accepted if it solved your problem :-) – Luis Mendo Dec 14 '13 at 00:21
  • 2
    This QA taught me something new (handling html in table data), I'll handle the upvote that Stephan can't. ;) – chappjc Dec 14 '13 at 07:24