How can I display a MATLAB table in a figure?

1 825 views (last 30 days)
I have a MATLAB table where each of the variables contains a single column of data.  How can I display the table in a figure?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Mar 2021
Edited: MathWorks Support Team on 5 Mar 2021
You can either use "uitable" or a more complicated series of commands depending on how you want the table to look.
For example, let us assume you have a table defined by the following code:
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);
1.) If you want the table to have adjustable-width columns and a more stylized look, execute the following "uitable" command to display the table "T" in a figure:
uitable('Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position',[0, 0, 1, 1]);
You can refer to the following documentation link for more information on "uitable":
2.) If you want the table to look very similar to how it looks when outputted in the MATLAB command window, execute the following series of commands to display the table "T" in a figure:
% Get the table in string form.
TString = evalc('disp(T)');
% Use TeX Markup for bold formatting and underscores.
TString = strrep(TString,'<strong>','\bf');
TString = strrep(TString,'</strong>','\rm');
TString = strrep(TString,'_','\_');
% 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]);
You may find the following documentation links useful:
  5 Comments
Daniel Leibundgut
Daniel Leibundgut on 29 Jul 2020
How can I adjust the displ,ay format of the data? Set the number of digits per column individually?

Sign in to comment.

More Answers (2)

Amir Pasha Zamani
Amir Pasha Zamani on 30 Sep 2020
Edited: Amir Pasha Zamani on 15 Mar 2021
The second method did´nt work for me bacause my table is very big and the look of it is not appropriate when it is shown in Mtalab workspace
The first method I need to run, but I get this error:
error:
Cannot concatenate the table variables 'Day_indx' and 'DATE', because their types are double and cell
I can not change the types of my variables, I needthem the way they are!
Any suggestion ?
  2 Comments
Guenter Troll
Guenter Troll on 9 May 2022
My table contains latex symbols. Consequently, using the second solution (first does not work at all) the rows of the table are shifted with respect to each other. I tried to add tabs but that does not work. They are ignored.

Sign in to comment.


Chris McGraw
Chris McGraw on 10 Oct 2022
If you were happy with Answer#2 above but don't want to construct the table in a dedicated figure/uifigure/uipanel, the same Tex-formatted string can be used with the text() function to output the table to an axis (e.g. as defined by subplot function).
Ax_stats= subplot(211);
set(Ax_stats,'YTickLabel',{},'XTickLabel',{},'XTick',[],'YTick',[]);
Ax_stats.XAxis.Visible = 'off';
Ax_stats.YAxis.Visible = 'off';
text(0.5,0.5,TString,'Units','normalized','HorizontalAlignment','center','FontName','Courier New','FontSize',8);
The data are not 'bound' by the confines of the axis, but it does put them there if you have a small handful of results to display.
I haven't had much luck getting fixed width font to work on Linux, but happy with the half-way result.

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by