Is there a way to make the largest numerical value in each row in uitable bold, automatically (as in if the dataset varies) ??

5 views (last 30 days)
For example if I run this code, I get a table 5x5 uitable, where the first column contains empty cell elements:
a = [cell(4,1) num2cell(rand(4))];
f = figure('Name', 'results', 'units', 'normalized');
t = uitable(f, 'Data', [a]),
t.Position(3) = t.Extent(3);
t.Position(4) = t.Extent(4);
I would like to have the largest numerical value in each row be bold in the uitable, and have it occur automatically whenever I input different dataset. How do I do this?? It is so that the viewer can quickly identify which column contains the largest value simply by looking at the uitable.
Thanks in advance!!

Answers (3)

Yair Altman
Yair Altman on 15 Jan 2016
The easy way (in the sense that you only need to use Matlab and a bit of HTML) is to add HTML tags in the cell contents, as shown by Walter above. This is easily done by adding the '<html>' prefix to the cell contents (as a string, not a numeric value), and then adding tags such as <b> (bold) or <i> (italic) or <font color="red"> (for color).
The more complex way (but faster and one that does not modify the table data) is to use a CellRenderer for the specific table column, that would highlight the cell contents based on its run-time value. I explained a real-world usage example here: http://undocumentedmatlab.com/blog/uitable-cell-colors
You can find out a lot more above both of these alternatives and other uitable customizations in my uitable customization report and my book Undocumented Secrets of Matlab-Java Programming

Walter Roberson
Walter Roberson on 30 Dec 2015
No, uitable() has no facility for that. You will need to code it.
%we need to convert empty cells to be something numeric so that cell2mat works properly
%so that we get the right column index.
%in the case where we *know* the leading column is empty it could be adjusted for
%without changing the empty to -inf
a_copy = a;
a_copy( cellfun(@isempty, a_copy) ) = {-inf};
%now find the maximum value by row, and the corresponding column index
[maxval, maxidx] = max(cell2mat(a_copy),[],2);
%construct the strings needed to bold those entries
boldval = arrayfun(@(V) sprintf('<HTML><B>%.16g</B>', V), maxval, 'Uniform', 0);
%put them into the appropriate location in a copy of the cell array
a_copy = a;
a_copy((maxidx-1)*size(a,1) + (1:size(a,1))') = boldval;
%and make that the presented uitable
set(t,'Data', a_copy);
The code I used for calculating the destination locations is an optimized code based upon how array indexing works in MATLAB. An alternate version that you might find clearer would be
a_copy( sub2ind(size(a), 1:size(a,1), maxidx) ) = boldval;
Note: if you allow editing of the uitable and the user clicks on the bold cell, then they will be allowed to edit the string including the '<HTML>' and all. This is not avoidable, at least not without going to the Java level. If you allow the cells to be edited and you need to read the user's value back in, then take care in reading the resulting strings!
And if you want the user to be able to edit the cells and have the new row maximum automatically highlighted then you have more work ahead of you...
  2 Comments
George Vuong
George Vuong on 3 Jan 2016
hm ok. Yeah it's a lot more complicated than I thought. I will have to try that method when I have time. Thanks for your response.
Walter Roberson
Walter Roberson on 3 Jan 2016
uitable() does not have cascading style sheets (CSS) or anything similar. It was never intended as a full-functionality display.
These days, there is an HTML standard, and there are 6 standards about various aspects of CSS -- that is how complex it has become to get a full modern display.

Sign in to comment.


Image Analyst
Image Analyst on 31 Dec 2015
That sounds like a question for Yair: http://undocumentedmatlab.com/?s=uitable

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!