for UITABLE: BackgroundColor for row

조회 수: 5 (최근 30일)
Youcef BOUNADJA
Youcef BOUNADJA 2013년 4월 12일
편집: Walter Roberson 2015년 12월 19일
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