使用HTML内容在Matlab中更改表格单元格的背景颜色 [英] Changing the background color of a table cell in matlab using html content

查看:345
本文介绍了使用HTML内容在Matlab中更改表格单元格的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道适用支持html内容
有关类似于我想要的示例的信息,请参见问题在使用matlab中的按钮的回调函数中的代码之前,我曾问过:

we know that uitable supports html content
for an example similar to what I want see here
to solve the problem I asked before I used this code in the callback of a button in 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);

我的问题是此行不起作用:

my problem is this line doesn't work:

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没有给出错误消息!!!
请注意,我什至使用了这段代码,但是没有变化.

I mean when I open the workspace in matlab I see that handles.tableData(handles.indexes,2) is set to the string.
but the background color does not change even this html code is not shown as a simple string. no change happens for the cell!!!
and matlab gives no error message!!!
Note that I even used this code but there was no change.

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

推荐答案

@Floris是正确的,字符串未作为MATLAB代码求值",您需要显式地编写颜色.这是一个小例子:

@Floris is correct, the string is not "evaluated" as MATLAB code, you need to explicitly write the colors. Here is a small example:

%# 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代码的一些变体.问题是背景颜色仅应用于文本,但没有填充整个表格单元格:

Note: I tried a few variations of the HTML code. The issue was that the background color was only applied to the text but did not fill the entire table cell:

<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子集是有限的.

The last one worked, but its not better than the previous code I showed. I tried other CSS tricks to fill the entire cell space, but failed. I think that the subset of HTML/CSS supported in Java Swing components is limited.

上述 HTML方法适用于小型表.对于较大的表或要启用编辑功能,可以使用更好的方法.它需要Java Swing熟悉.

The above HTML approach works fine for small tables. For larger tables or when you want to enable editing, there is a better approach. It requires Java Swing familiarity.

这篇关于使用HTML内容在Matlab中更改表格单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆