Table PDF 报告生成器中格式的字段 - Matlab

Table fields in format in PDF report generator - Matlab

当我将 table 保存为 PDF(使用报告生成器)时,我没有得到数字字段(索引 1、索引 2、索引 3)shortG 格式(总共 5 位)。有什么问题,我该如何解决?

代码:

function ButtonPushed(app, event)
        import mlreportgen.dom.*;
        import mlreportgen.report.*

        format shortG;
        ID = [1;2;3;4;5];
        Name = {'San';'John';'Lee';'Boo';'Jay'};
        Index1 = [71.1252;69.2343245;64.345345;67.345322;64.235235];
        Index2 = [176.23423;163.123423654;131.45364572;133.5789435;119.63575647];
        Index3 = [176.234;16.123423654;31.45364572;33.5789435;11.6647];
        mt = table(ID,Name,Index1,Index2,Index3);


        d = Document('myPDF','pdf');
        d.OutputPath = ['E:/','temp'];

        append(d,'Table 1: '); 
        append(d,mt); 
        close(d);
        rptview(d.OutputPath); 
end

要解决此问题,请在写入 PDF 之前将数值数组格式化为具有 5 位有效数字的字符数组。

mt = table(ID,Name,f(Index1),f(Index2),f(Index3));

其中,

function FivDigsStr = f(x)
%formatting to character array with 5 significant digits and then splitting. 
%at each tab. categorical is needed to remove ' 's that appear around char 
%in the output PDF file with newer MATLAB versions
%e.g. with R2018a, there are no ' ' in the output file but ' ' appears with R2020a
FivDigsStr = categorical(split(sprintf('%0.5G\t',x)));
%Removing the last (<undefined>) value (which is included due to \t)
FivDigsStr = FivDigsStr(1:end-1);
end

以上更改产生以下结果:


编辑:

要带回 headers:

mt.Properties.VariableNames(3:end) = {'Index1', 'Index2', 'Index3'};

或者以更通用的方式提取变量名而不是对它们进行硬编码,您可以使用 inputnames 来提取变量名。

V = @(x) inputname(1);
mt.Properties.VariableNames(3:end) = {V(Index1), V(Index2), V(Index3)};

给出: