how to change charactors of the table which is made by uitable by codes
Show older comments
I use the below code to get a table as below:
f=figure('Position',[0 60 130+C*80,30+R*20]);
t=uitable(f,'Data',Cdata,...
'ColumnName',cname,...
'RowName',rname);

Now I want to change the 5th row's font and background by adding some codes, shall I?
Thank you!
Answers (1)
Walter Roberson
on 16 Dec 2015
row5 = cellfun(@(S) sprintf('<HTML><FONT face="helvetica" color="red">%s</FONT>', Cdata(5,:);
new_Cdata = Cdata;
new_Cdata(5,:) = row5;
set(t, 'Data', new_Cdata);
6 Comments
vx2008
on 16 Dec 2015
Walter Roberson
on 16 Dec 2015
new_Cdata = Cdata;
row1_4 = cellfun(@(x) sprintf('<HTML><TABLE><TD color="red" bgcolor="yellow">%.4g</TD></TABLE>', x), Cdata(1:4,:), 'Uniform', 0);
row5 = cellfun(@(S) sprintf('<HTML><TABLE><TD color="blue" bgcolor="red"><FONT size="+1">%s</FONT></TD></TABLE>', S), Cdata(5,:), 'Uniform', 0);
row6plus = cellfun(@(x) sprintf('<HTML><TABLE><TD color="yellow" bgcolor="red">%.4g</TD></TABLE>', x), Cdata(6:end,:), 'Uniform', 0);
new_Cdata(1:4,:) = row1_4;
new_Cdata(5,:) = row5;
new_Cdata(6:end,:) = row6plus;
set(t, 'Data', new_Cdata);
You might find that you want to go further and set a fixed-width font and that you want to pad with to get the color boundaries to line up nicely.
Walter Roberson
on 18 Dec 2015
You are changing the rules as you go along :(
You presented your columns as all being numeric except for row 5. Now some of them are string above a point and numeric below that point. And you have not followed any rule that is at all obvious about how many decimal places to use, having left 161.4476 as 4 decimal places but converted the 1.6211e+03 to 1.62E+03
In any case you have now been shown how to use cellfun() on elements and how to sprintf elements with numeric or string format in order to add foreground and background color. You can put those together to write a function that tests whether the input is string or numeric and uses the appropriate format and adds the colors desired.
I tested with using various combinations of TABLE TR TD. The most reliably formatted version was using TABLE with TD, or using the full TABLE TR TD structure. Other versions sometimes do not get fixed width areas right or sometimes drop everything after the first space.
To get fixed width areas you can use use PRE . If you use FONT with face="courier" that does not always give the same spacing as face="fixed" in my tests.
In order to have solid color, the color needs to be imposed by a higher level HTML element that includes all of the elements to be colored. This means you can use TABLE bgcolor and a bunch of TD or a TR and a bunch of TD, or you can use TABLE TR bgcolor with a bunch of TD, but if you use TD bgcolor then adjacent TD will have a white break between them. Another alternative is to use TABLE TR or TABLE TD or TABLE TR TD, then PRE, and put the entire text for one row with a single space between the columns.
To state a bit more clearly: as long as you are using uitable with Data that has individual cells for the elements of the row, you are going to end up with broken color between the columns. Every cell entry in uitable is rendered separately, its own HTML context.
vx2008
on 18 Dec 2015
Walter Roberson
on 19 Dec 2015
For making each column the same blank-padded length see http://www.mathworks.com/matlabcentral/answers/71812-for-uitable-backgroundcolor-for-row#comment_142953
Categories
Find more on Elementary Math 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!


