【问题标题】:Table fields in format in PDF report generator - MatlabPDF报告生成器中格式的表格字段 - Matlab
【发布时间】:2020-05-08 06:18:50
【问题描述】:

当我将表格另存为 PDF(使用报告生成器)时,我没有得到数字字段(Index1、Index2、Index3)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

【问题讨论】:

    标签: matlab pdf matlab-app-designer


    【解决方案1】:

    要解决此问题,请在写入 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
    

    上述更改产生以下结果:


    编辑:

    要带回标题:

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

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

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

    给出:

    【讨论】:

    • 谢谢。它们被转换为字符串并且它们丢失了标题。是否可以将其转换回数字并带回标题?
    • 非常感谢。知道如何将它们恢复为数字吗?
    • @Iroca 对于写入 PDF,无论是数字还是字符串都没有任何区别
    • 确实如此,但如何省略表示数字的“”?
    • @Iroca 输出的 PDF 文件中没有 ' '
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多