for UITABLE: BackgroundColor for row
11 views (last 30 days)
Show older comments
Youcef BOUNADJA
on 12 Apr 2013
Edited: Walter Roberson
on 19 Dec 2015
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;
0 Comments
Accepted Answer
Walter Roberson
on 12 Apr 2013
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 Comments
Walter Roberson
on 12 Apr 2013
Edited: Walter Roberson
on 19 Dec 2015
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);
More Answers (1)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!