1

I referred to this answer in coloring specific rows of a table in the GUI, however, I get some weird symbols instead of actual numbers present in those rows as shown below: enter image description here

This is the line of code I am using to color:

DataTable = [num2cell(handles.zRaw), num2cell(handles.pRaw), num2cell(handles.zMob),...
            num2cell(handles.PressGrubbs), num2cell(handles.PressRosner), handles.OutlCheckGRubbs,...
            handles.OutlCheckRosner, num2cell(handles.iZones), num2cell(handles.iExcessPress)];

        %# Use HTML to style these cells
        n = 1:size(DataTable, 2);
        DataTable(idx, n) = strcat('<html><span style="color: #FF0000; font-weight: bold;">',...
            DataTable(idx, n));

Additionally, I also get this warning:

Warning: Out of range or non-integer values truncated during conversion to character.

In cell.strcat at 55

In the above DataTable, variables handles.OutlCheckGRubbs and handles.OutlCheckRosner are array of strings.

Community
  • 1
  • 1

1 Answers1

3

The issue is that your table (cell-array) contains both numeric and string data. When you use strcat it treats all its input as strings, which means that numeric data is truncated and treated as ASCII/Unicode code points. Example:

%# note that double('d')==100
>> strcat(100.6,'aaa')
ans =
daaa

The warning you see is because MATLAB really only supports the first 2^16 characters codepoints (BMP plane of UTF-16/UCS-2):

>> strcat(2^16 + 100, 'a')
Warning: Out of range or non-integer values truncated during conversion to character. 
> In strcat at 86 
ans =
a

What you should be doing then is to convert numbers to strings first:

>> strcat(num2str(100), 'a')
ans =
100a

EDIT:

Here is an example that resembles your code. Note how numeric columns had to be converted to strings first:

%# data columns you have. Some are numeric, others are strings
col1 = rand(10,1);
col2 = repmat({'ok'},10,1);
col3 = randi(100, 10,1);

%# combine into a table cell-array (all strings)
convert = @(x) strtrim(cellstr(num2str(x)));
table = [convert(col1) col2 convert(col3)];

%# apply custom formatting to some rows
idx = rand(10,1)>0.7;
table(idx,:) = strcat('<html><span style="color: red;">', table(idx,:));

%# show uitable
uitable('Data',table)

screenshot

One thing to note is that UITABLE displays strings left-aligned, while numbers are displayed right-aligned. So by converting numbers to strings, we get a different text alignment.

The conversion numeric->string was performed using NUM2STR. You could customize the call to specify exactly how many digits to display if you want, as in: num2str(10.01, '%.6f')


EDIT2:

In response to comment, here is one way to assign different colors:

idx = [1 4 5 9];
clr = {'red'; 'green'; 'rgb(0,0,255)'; '#FF00FF'};
table(idx,:) = strcat('<html><span style="color: ', ...
    clr(:,ones(1,size(table,2))), ...
    ';">', table(idx,:));

For simplicity, I assume 4 colors matching 4 rows.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • I convert my 7 double arrays to string arrays (not cell array as shown in question) using `num2str` and 2 cell arrays to string arrays using `char`. However, the size of 7 arrays (`50 x 1`), converted from numbers, is not compatible with size of other 2 arrays (`50 x 7`), converted from cells, for string concatenation; hence, I get an error `Subscripted assignment dimension mismatch.` Any idea how I can make the dimensions consistent? –  Jul 08 '13 at 18:32
  • 1
    @Pupil: I added an example which shows how to concatenate the columns while converting the numeric ones to strings – Amro Jul 08 '13 at 18:55
  • If instead of red I want to assign 4 different colors (say `clr = {'r', 'g', 'b', 'm'}`) to 4 defined indices (say, `idx = [1, 4, 5, 9]`), respectively, then how can I define these color codes in html efficiently without using loop perhaps? –  Jul 08 '13 at 19:53
  • @Pupil: see my edit. If you dont have the same number of colors as rows, you'll have to write some code to "cycle" the colors – Amro Jul 08 '13 at 20:07
  • For some reason I had to use loop to pull up the indices I required for coloring, hence I used this: `clrHTML = {'red', 'blue', 'green', 'magenta', 'yellow', 'cyan'}; for iZ = 1:max(handles.iZones) idx = (handles.iZones == iZ); DataTable(idx, :) = strcat([''],... DataTable(idx, :)); end` ...thanks! –  Jul 08 '13 at 20:27