Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top