How can I save my uitable()
to a PDF, JPG, etc.?
Using Matlab R2018a, no fancy add-ons. Looking for solutions using tables, not arrays, due to size/complexity of the real data.
% construct simple table as example %
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);
% what command to save to PDF? or JPG, PNG, whatever
Note that f = figure
doesn't work, otherwise it would be easy to save as PDF (I think it is having trouble passing the table t
, I'm not sure):
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.
You can create it as a figure
instead of a uifigure
to save it. But you cannot pass a table to it. You need a simple array or a cell-array for that. Since you have mixed data type, therefore cell-array is the way to go. Use table2cell
to convert your table to a cell-array.
f = figure;
uit = uitable(f, 'Data', table2cell(t));
uit.ColumnName={t.Properties.VariableNames{:}}; %renaming columns to that of table
uit.RowName=[]; %removing default row numbering as in your uitable
Now you are good to go with saving your figure in any of your desired formats. e.g:
saveas(f, 'q60974307.png');
Result: