1

我在 GUI 中为表格的特定行着色时提到了这个答案,但是,我得到了一些奇怪的符号,而不是这些行中存在的实际数字,如下所示: 在此处输入图像描述

这是我用来着色的代码行:

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));

此外,我还收到此警告:

警告:在转换为字符期间,超出范围或非整数值被截断。

在 55 处的 cell.strcat 中

在上面DataTable,变量handles.OutlCheckGRubbshandles.OutlCheckRosner是字符串数组。

4

1 回答 1

3

问题是您的表(单元格数组)包含数字和字符串数据。当您使用strcat它时,它将所有输入视为字符串,这意味着数字数据被截断并被视为 ASCII/Unicode 代码点。例子:

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

您看到的警告是因为 MATLAB 真的只支持前 2^16 个字符的代码点(UTF-16/UCS-2 的 BMP 平面):

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

那么你应该做的是首先将数字转换为字符串:

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

编辑:

这是一个类似于您的代码的示例。请注意如何必须首先将数字列转换为字符串:

%# 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)

截屏

需要注意的一件事是 UITABLE 显示字符串左对齐,而数字显示右对齐。因此,通过将数字转换为字符串,我们得到了不同的文本对齐方式。

使用 NUM2STR 执行数字->字符串的转换。如果需要,您可以自定义调用以准确指定要显示的位数,如下所示:num2str(10.01, '%.6f')


编辑2:

作为对评论的回应,这是分配不同颜色的一种方法:

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,:));

为简单起见,我假设 4 种颜色匹配 4 行。

于 2013-07-08T17:22:22.953 回答