for UITABLE: BackgroundColor for row

8 次查看(过去 30 天)
hello,
i have problem in GUI, for UITABLE the BackgroundColor are for every row but i want BackgroundColor for every colomn please help me,
.
for exemple:
f = figure('Position', [100 100 752 350]); t = uitable('Parent', f, 'Position', [25 25 700 200]);
set(t, 'Data', magic(10));
foregroundColor = [1 1 1]; set(t, 'ForegroundColor', foregroundColor); backgroundColor = [.4 .1 .1; .1 .1 .4]; set(t, 'BackgroundColor', backgroundColor);
.
thank you in advance
faithfully;

采纳的回答

Walter Roberson
Walter Roberson 2013-4-12
You cannot directly set colors per column (unless it can be done at the Java level.)
The work-around is to change the column to string format, convert the numeric values to string, and wrap each string with an HTML code to change the color.
bgcols = floor(255 * [.4 .1 .1;.1 .1 .4]);
colfmtstr = sprintf('<HTML><TABLE><TD bgcolor="rgb(%d,%d,%d)"%%g', bgcols(1,:));
colcontents = cellstr( num2str(magic(10), colfmtstr) );
then make colcontents one of the columns of the data. Change the color for a different column by changing the (1,:) to (2,:)
  2 个评论
Youcef BOUNADJA
Youcef BOUNADJA 2013-4-12
im very sorry but,
can you give me that code in an exemple... because that's not work
thank you again
Walter Roberson
Walter Roberson 2013-4-12
编辑:Walter Roberson 2015-12-19
Sorry, I forgot that num2str() needs data in columns to work the way I was thinking of. Also, I did some work on getting the columns to look better.
f = figure('Position', [100 100 752 350]);
t = uitable('Parent', f, 'Position', [25 25 700 200], 'FontName', 'courier'); %fixed-width font but not 'fixed' itself
desiredcellwid = 10; %adjust as needed
fgcols = floor(255 * [1 1 1]);
bgcols = floor(255 * [.4 .1 .1;.1 .1 .4]);
colprestr = sprintf('<HTML><TABLE><TD color="rgb(%d,%d,%d)" bgcolor="rgb(%d,%d,%d)">', fgcols(1,:), bgcols(1,:));
numfmt = '%g';
msquare = magic(10);
for K = 1 : size(msquare,2)
Tcol = cellstr(num2str(msquare(:,K), numfmt));
Tcol = cellfun(@(s) [blanks(desiredcellwid - length(s)), s], Tcol, 'Uniform', 0);
Tcol = strcat( colprestr, regexprep(Tcol, ' ', ' ') );
colcontents(:,K) = Tcol;
end
set(t, 'Data', colcontents);

请先登录,再进行评论。

更多回答(1 个)

Youcef BOUNADJA
Youcef BOUNADJA 2013-4-12
i want to thanks you for your answer so quickly
i will try right now

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by