MATLAB: How to display a MATLAB table in a figure and make it bold

allboldevalcMATLABtableuistyleuitable

How can I display a MATLAB table in a figure and make everything in it (column headers and data) appear bold?

Best Answer

To display a table in a figure, see the following MATLAB Answers post, which outlines two methods, one using "uitables" and another printing the Command Window output as an "annotation" with TeX Markup on the figure:
<https://www.mathworks.com/matlabcentral/answers/254690-how-can-i-display-a-matlab-table-in-a-figure>
If you then want to make the data in the table itself appear bold (not just the columns header), try the following for the two methods described in the above link:
1) With uitables, you can format individual cells using "uistyle" objects, then add that format to the table with the "addStyle" function:
<https://www.mathworks.com/help/matlab/ref/uistyle.html?s_tid=doc_ta>
<https://www.mathworks.com/help/matlab/ref/matlab.ui.control.tableappd.addstyle.html?s_tid=doc_ta>
2) If you are using an annotation with TeX Markup to display the Command Window output of the table, add your "\bf" and "\rm" tags to the beginning and end of your text, respectively. This will make all text appear bold. Below I have edited the code block from the MATLAB Answers post linked above,
% Create table
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
T = table(Age,Height,Weight,'RowNames',LastName);
% Get the table in string form.
TString = evalc('disp(T)');
% Use TeX Markup for bold formatting and underscores.
TString = strrep(TString,'<strong>',''); %these tags are now redundant
TString = strrep(TString,'</strong>',''); %these tags are now redundant
TString = strrep(TString,'_','\_');
TString = ['\bf' TString '\rm']; %add bold markup tags to beginning and end of text
% Get a fixed-width font.
FixedWidth = get(0,'FixedWidthFontName');
% Output the table using the annotation command.
annotation(gcf,'Textbox','String',TString,'Interpreter','Tex',...
'FontName',FixedWidth,'Units','Normalized','Position',[0 0 1 1]);