How to save uitable() to PNG

57 次查看(过去 30 天)
newbie9
newbie9 2020-3-31
回答: Swaroop 2022-11-5
I am trying to use uitable() to convert my Table to a PNG, but I'm having trouble saving it.
Using R2018a, no fancy add-ons
t = array2table(zeros(4,1));
t.Properties.VariableNames{'Var1'} = 'id';
t.id(:) = 1;
t.fieldA = {'a'; 'b'; 'c'; 'd'};
t.GroupCount = [22; 1; 19; 0];
f = uifigure;
uit = uitable(f, 'Data', t);
Note that f = figure doesn't work, otherwise it would be easy to save as PNG:
f=figure;
uit = uitable(f, 'Data', t);
Error using uitable
Functionality not supported with figures created with the figure function. For more information, see Graphics Support in App Designer.

回答(2 个)

newbie9
newbie9 2020-4-1
I asked on StackOverflow and got an answer that worked:
f = figure;
uit = uitable(f, 'Data', table2cell(t));
uit.ColumnName={t.Properties.VariableNames{:}};
uit.RowName=[]; %removing default row numbering as in your uitable
saveas(f, 'figname.png');
As explained in the answer on StackOverflow, f = figure with uitable requires a cell array, not a table.

Swaroop
Swaroop 2022-11-5
There is a better solution now if you are using version 2020b or higher
Instead of figure handle use a uifigure handle. Add all the styling you want and export the uifigure as image or PDF using exportapp(). This method is much more flexible than using a figure handle.

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by