如何将 uitable() 保存为 PDF、JPG 等格式

How to save uitable() to PDF, JPG, etc

如何将我的 uitable() 保存为 PDF、JPG 等文件?

使用 Matlab R2018a,没有花哨的插件。由于 size/complexity 的真实数据,正在寻找使用 table 而非数组的解决方案。

    % 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

请注意,f = figure 不起作用,否则保存为 PDF 很容易(我认为通过 table t 时遇到问题,我不是当然):

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.

您可以将其创建为 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 以将您的 table 转换为元胞数组。

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

现在您可以saving您想要的任何格式的图形了。例如:

saveas(f, 'q60974307.png');

结果: