0

我们知道uitable支持 html 内容
的示例类似于我想在这里
看到的, 以解决在 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);

我的问题是这条线不起作用:

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

我的意思是当我在 matlab 中打开工作区时,我看到 handle.tableData(handles.indexes,2) 设置为字符串。
但即使此 html 代码未显示为简单字符串,背景颜色也不会改变。细胞没有变化!!!
并且matlab没有给出错误信息!!!
请注意,我什至使用了此代码,但没有任何变化。

handles.tableData(handles.index,2)=cellstr('<html><span style="background-color:  #FF0000;"></span></html>');
4

2 回答 2

5

@Floris 是正确的,字符串没有“评估”为 MATLAB 代码,您需要明确编写颜色。这是一个小例子:

%# 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',[]);

截屏


注意:我尝试了一些 HTML 代码的变体。问题是背景颜色只应用于文本,但没有填充整个表格单元格:

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

截屏

最后一个工作,但它并不比我之前展示的代码好。我尝试了其他 CSS 技巧来填充整个单元格空间,但失败了。我认为 Java Swing 组件中支持的 HTML/CSS 子集是有限的。


上述HTML 方法适用于小表格。对于较大的表格或当您想要启用编辑时,有一种更好的方法。它需要熟悉 Java Swing。

于 2013-07-06T15:58:03.543 回答
3

比较您的代码(我添加了换行符以提高可读性 - 考虑这些“单行”):

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

使用链接中的代码

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

有一个非常重要的区别。在原始代码中,颜色被定义为一个十六进制数字(可以在呈现 HTML 时进行解释)。在您的代码中,该color变量是 Matlab 已知的 - 但在您创建tableData. 而且 HTML 解释器在遇到它时不知道该怎么做,color(1,1)所以它会默默地忽略整个命令。要解决此问题,您需要确保以“有意义”结尾的字符串 - 即转换color为字符串。注意 - 当我查看它的输出时uisetcolor,返回的值似乎在 and 之间01而不是 between 0and 255;所以你想先将颜色值乘以 255:

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

此时,colorStringrgb(173,235,255)(例如)。

现在您可以将整个格式字符串创建为

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

你可以设置它:

handles.tableData(handles.index,2) = cellstr(formatString);
于 2013-06-18T22:00:00.043 回答