0

we know that uitable supports html content
for an example similar to what I want see here
to solve the problem I asked before I used this code in the callback of a button in matlab:

color = uisetcolor;  
numberOfClasses = str2num(get(handles.edtNumClass,'String'));  
if handles.index == 0  
    handles.tableData = cell(numberOfClasses,2);
    guidata(hObject,handles);
end
handles.index = handles.index+1;
handles.tableData(handles.index,1)=cellstr(get(handles.edtNameClass,'String'));
handles.tableData(handles.index,2)=cellstr('<html><span style="background-color:  rgb(color(1,1),color(1,2),color(1,3));"></span></html>');
set(handles.uitable2,'data',handles.tableData);

my problem is this line doesn't work:

handles.tableData(handles.index,2)=cellstr('<html><span style="background-color:  rgb(color(1,1),color(1,2),color(1,3));"></span></html>');

I mean when I open the workspace in matlab I see that handles.tableData(handles.indexes,2) is set to the string.
but the background color does not change even this html code is not shown as a simple string. no change happens for the cell!!!
and matlab gives no error message!!!
Note that I even used this code but there was no change.

handles.tableData(handles.index,2)=cellstr('<html><span style="background-color:  #FF0000;"></span></html>');
Community
  • 1
  • 1
Sepideh Abadpour
  • 2,550
  • 10
  • 52
  • 88

2 Answers2

5

@Floris is correct, the string is not "evaluated" as MATLAB code, you need to explicitly write the colors. Here is a small example:

%# data
X = {
    'Alice'   1
    'Bob'     2
    'Charlie' 3
    'Dave'    4
};

%# get color from user
c = uisetcolor();

%# format color as: rgb(255,255,255)
%#clr = sprintf('rgb(%d,%d,%d)', round(c*255));

%# format color as: #FFFFFF
clr = dec2hex(round(c*255),2)'; clr = ['#';clr(:)]';

%# apply formatting to third row first column
X(3,1) = strcat(...
    ['<html><body bgcolor="' clr '" text="#FF0000" width="100px">'], ...
    X(3,1));

%# display table
f = figure('Position',[100 100 350 150]);
h = uitable('Parent',f, 'ColumnWidth',{100 'auto'}, ...
    'Units','normalized', 'Position',[0.05 0.05 0.9 0.9], ...
    'Data',X, 'ColumnName',{'Name','Rank'}, 'RowName',[]);

screenshot


Note: I tried a few variations of the HTML code. The issue was that the background color was only applied to the text but did not fill the entire table cell:

<html><span style="background-color: #FFFF00; color: #FF0000;">

<html><font style="background-color: #FFFF00; color: #FF0000;">

<html><table cellpadding="0" width="100px" bgcolor="#FFFF00" style="color: #FF0000;"><tr><td>

screenshot

The last one worked, but its not better than the previous code I showed. I tried other CSS tricks to fill the entire cell space, but failed. I think that the subset of HTML/CSS supported in Java Swing components is limited.


The above HTML approach works fine for small tables. For larger tables or when you want to enable editing, there is a better approach. It requires Java Swing familiarity.

Amro
  • 123,847
  • 25
  • 243
  • 454
3

Comparing your code (I added line breaks for readability - consider these "on one line"):

handles.tableData(handles.index,2)=  ...
  cellstr('<html>
           <span style="background-color: rgb(color(1,1),color(1,2),color(1,3));">
           </span></html>');

With the code from your link

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

There is a Very Important Difference. In the original code, the color is defined as a hex number (which can be interpreted when the HTML is rendered). In your code, the color variable is known to Matlab - but it is treated as a string when you create the tableData. And the HTML interpreter doesn't know what to do when it comes across color(1,1) so it silently ignores the whole command. To fix this, you need to make sure that the string you end up with "makes sense" - i.e. convert color to a string. Note - when I looked at the output of uisetcolor it appeared that the value returned was between 0 and 1, not between 0 and 255; so you want to multiply the color value by 255 first:

c255 = color(1,1:3)*255;
colorString = sprintf('rgb(%d,%d,%d)', c255);

At this point, colorString is rgb(173,235,255) (for example).

Now you can create your entire formatstring as

formatString = ['<html><span style="background-color: ' colorString ';"></span></html>'];

And you can set it:

handles.tableData(handles.index,2) = cellstr(formatString);
Floris
  • 45,857
  • 6
  • 70
  • 122
  • I used your code but no change happened and I told in the question that even using hex numbers does not make sense – Sepideh Abadpour Jun 18 '13 at 22:21
  • when I open the workspace I see that cell's value is set to '' but it does not render when I write the line set(handles.uitable2,'data',handles.tableData) – Sepideh Abadpour Jun 18 '13 at 22:24
  • Strange. I can't test this right now as I don't have Matlab at home... can you get any other html formatting to work (for example by following the example you linked above)? Does it make a difference whether there is anything in the cell (any text, vs "nothing" in this case)? – Floris Jun 18 '13 at 23:26
  • oh yes I tested and understood that I can change the forground color of cell in this way but not the background color – Sepideh Abadpour Jun 19 '13 at 00:14
  • According to [this earlier answer](http://stackoverflow.com/questions/12918923/matlab-set-background-color-in-a-cell-of-uicontrol) the property name is `bgcolor` and the over all syntax used is slightly different! Let me know if that works then I will update the answer. – Floris Jun 19 '13 at 01:53
  • you know in [this earlier answer](http://stackoverflow.com/questions/12918923/matlab-set-background-color-in-a-cell-of-uicontrol) CSS is not used in the HTML code but I have used HTML in my own code – Sepideh Abadpour Jun 19 '13 at 16:48
  • Well I have written this code and I'm sure that this is write because I have tested it in mozilla but it does not work in matlab. I mean background color of the cell does not change.c255 = handles.color(handles.index,1:3)*255; colorString = sprintf('rgb(%d,%d,%d)', c255); formatString=strcat(''); handles.tableData(handles.index,1)=cellstr(get(handles.edtNameClass,'String')); handles.tableData(handles.index,2)=cellstr(formatString); – Sepideh Abadpour Jun 19 '13 at 20:36
  • What version of Matlab are you using? – Floris Jun 20 '13 at 00:30