【问题标题】:Changing the background color of a table cell in matlab using html content使用html内容更改matlab中表格单元格的背景颜色
【发布时间】:2013-06-15 07:01:04
【问题描述】:

我们知道uitable支持html内容
对于类似于我想要的示例,请参见 here
解决problem我在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 中打开工作区时,我看到handles.tableData(handles.indexes,2) 被设置为字符串。
但背景颜色不会改变 甚至这个 html 代码也没有显示为简单的字符串。 单元格没有变化!!!
而且matlab没有给出错误信息!!!
请注意,我什至使用了此代码,但没有任何变化。

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

【问题讨论】:

    标签: matlab user-interface cell background-color matlab-uitable


    【解决方案1】:

    @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 approach 适用于小桌子。对于较大的表格或当您想要启用编辑时,有一个better approach。它需要熟悉 Java Swing。

    【讨论】:

      【解决方案2】:

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

      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 时它被视为字符串。当遇到color(1,1) 时,HTML 解释器不知道该怎么做,所以它会默默地忽略整个命令。要解决此问题,您需要确保以“有意义”结尾的字符串 - 即将 color 转换为字符串。注意 - 当我查看uisetcolor 的输出时,返回的值似乎在01 之间,而不是在0255 之间;所以你想先将颜色值乘以 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);
      

      【讨论】:

      • 我使用了你的代码,但没有发生任何变化,我在问题中说即使使用十六进制数字也没有意义
      • 当我打开工作区时,我看到单元格的值设置为 ''但是当我写行 set(handles.uitable2,'data',handles.tableData)
      • 奇怪。我现在无法测试这个,因为我家里没有 Matlab……你能得到任何其他的 html 格式吗(例如按照你上面链接的例子)?单元格中是否有任何内容(任何文本,在这种情况下为“无”)是否有区别?
      • 哦,是的,我测试并了解我可以通过这种方式更改单元格的前景色,但不能更改背景色
      • 根据this earlier answer,属性名称为bgcolor,使用的所有语法略有不同!让我知道这是否有效,然后我会更新答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多