how to change charactors of the table which is made by uitable by codes

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)

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

I just test this code, but there is something wrong.
Maybe my question is not very clear, I modify it as below:
Cdata =
[ 1] [1] [ 1] [53.1100] [ 0]
[ 1] [2] [ 2] [59.0900] [ 0]
[ 2] [1] [ 2] [71.3100] [ 0]
[ 2] [2] [ 1] [70.2400] [ 0]
'SS' 'f' 'MS' 'F' 'Fx'
[215.3556] [1] [215.3556] [35.7317] [ 161.4476]
[ 6.0270] [1] [ 6.0270] [ 1] [4.0522e+03]
[ 12.4256] [1] [ 12.4256] [ 2.0617] [1.6211e+04]
[ 0] [0] [ 0] [ 0] [ 0]
[233.8083] [3] [ 0] [ 0] [ 0]
rname =
Columns 1 through 9
'2501-1' '2501-2' '2401-1' '2401-2' 'Va_Source' 'as' 'df' 'fsd' 'Error'
Column 10
'To_Variation'
>> cname
cname =
'as' 'df' 'fsd' 'fd'
f=figure('Position',[0 60 130+C*80,30+R*20]);
t=uitable(f,'Data',Cdata,...
'ColumnName',cname,...
'RowName',rname);
Then I get the above 'table'. Now I want to change something about this table;
fisrt set the 5th line of this table red background and blue charactors with bigger font;
second, set the lines above 5th as yellow background and red charactors; and lines below 5th as red background and yellow charators.
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.
Thank you for your information and your time, from which I learn much more;
I test the codes and find it is generally ok;
Cdata={'30 min' '1.5 g' '45℃' 53.11 0;'30 min' '2.0 g' '55℃' 59.09 0;'60 min' '1.5 g' '55℃' 71.31 0;'60 min' '2.0 g' '45℃' 70.24 0;...
'SS' 'f' 'MS' 'F' 'Fx' ;215.3556 1 215.3556 35.7317 161.4476;...
12.4256 1 12.4256 2.0617 1.6211e+03;0 0 0 0 0 ;233.8083 3 0 0 0;}
Cdata =
'30 min' '1.5 g' '45℃' [53.1100] [ 0]
'30 min' '2.0 g' '55℃' [59.0900] [ 0]
'60 min' '1.5 g' '55℃' [71.3100] [ 0]
'60 min' '2.0 g' '45℃' [70.2400] [ 0]
'SS' 'f' 'MS' 'F' 'Fx'
[215.3556] [ 1] [215.3556] [35.7317] [ 161.4476]
[ 12.4256] [ 1] [ 12.4256] [ 2.0617] [1.6211e+03]
[ 0] [ 0] [ 0] [ 0] [ 0]
[233.8083] [ 3] [ 0] [ 0] [ 0]
Then I type the below code to get the figure:
f=figure;
t=uitable(f,'data',Cdata);
t.Position(3)=t.Extent(3);
t.Position(4)=t.Extent(4);
After that I get the table as below picture 1:
Then I type the below code which you give me(change a little):
new_Cdata = Cdata;
row1_4 = cellfun(@(x) sprintf('<HTML><TABLE><TD color="red" bgcolor="yellow">%s</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);
Above table becomes another one as below picture 2:
In addition, about the color of background I want to get effect as below picture:
And I also want to delete all '0' in the table if feasible, which means if the cell is '0', never shown as below picture:
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.
Ok, I get it
Thank you for your guiding.
Sorry for any confusing to you.

Sign in to comment.

Categories

Find more on Elementary Math in Help Center and File Exchange

Asked:

on 16 Dec 2015

Commented:

on 19 Dec 2015

Community Treasure Hunt

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

Start Hunting!