我在GUI中为表格的特定行着色时提到this answer,但是,我得到了一些奇怪的符号,而不是这些行中存在的实际数字,如下所示:
这是我用来着色的代码行:
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.OutlCheckGRubbs
和handles.OutlCheckRosner
是字符串数组。
答案 0 :(得分:2)
问题是您的表(单元格数组)包含数字和字符串数据。当您使用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')
在回应评论时,这是分配不同颜色的一种方法:
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行匹配。