主要内容

本页采用了机器翻译。点击此处可查看英文原文。

根据 MATLAB 应用程序的结果生成演示文稿

此示例展示如何使用 MATLAB® PowerPoint® API (PPT API) 从 MATLAB 应用程序的结果生成 Microsoft® PowerPoint® 演示文稿。该示例根据预测美国人口的应用程序的结果生成演示文稿。以下是示例生成的幻灯片:

Five presentation slides created by the example, starting with the title slide, a slide with bullet points describing the population modeling approach, and three slides with MATLAB generated graphs.

创建演示文稿

导入 PPT 命名空间,这样您就不必使用长而完全限定的名称。

import mlreportgen.ppt.*;

为了方便删除示例生成的图像,请创建一个元胞数组来保存图像。

images = {};

使用默认模板创建演示文稿。

ppt = Presentation("population.pptx");
open(ppt);

将幻灯片添加到演示文稿中

PowerPoint 演示文稿由根据预定义布局创建的幻灯片组成。布局包含您用生成的内容填充的占位符。预定义布局属于定义样式的模板幻灯片母版。

使用 Title Slide 布局将第一张幻灯片添加到演示文稿中。

slide1 = add(ppt,"Title Slide");

使用 replace 方法替换幻灯片中的标题和副标题。

replace(slide1,"Title","Modeling the US Population");
replace(slide1,"Subtitle","A Risky Business");

使用 Title and Content 布局将第二张幻灯片添加到演示文稿中。更换标题。

slide2 = add(ppt,"Title and Content");
replace(slide2,"Title","Population Modeling Approach");

使用元胞数组将文本添加到 Content 占位符。

replace(slide2,"Content",{ ...
    "Fit polynomial to US Census data", ...
    "Use polynomials to extrapolate population growth", ...
    "Based on Computer Methods for Mathematical Computations" + ...
    " by Forsythe, Malcolm and Moler," + ...
    " published by Prentice-Hall in 1977", ...
    "Varying polynomial degree shows riskiness of approach"});

使用 Title and Content 布局将第三张幻灯片添加到演示文稿中。更换标题。

slide3 = add(ppt,"Title and Content");
replace(slide3,"Title","US Census data from 1900 to 2000");

创建 1910 年至 2000 年美国人口普查数据的图。

% Time interval
t = (1910:10:2000)';

% Population
p = [91.972 105.711 123.203 131.669 150.697...
    179.323 203.212 226.505 249.633 281.422]';

% Plot
fig1 = figure;
plot(t,p,"bo");
axis([1910 2020 0 400]);
title("Population of the US 1910-2000");
ylabel("Millions");

将绘图转换为图像。将图像添加到演示文稿生成结束时要删除的图像列表中。在关闭演示文稿之前,您不能删除图像。

img1 = "plot1.png";
saveas(fig1,img1);

Figure contains an axes object. The axes object with title Population of the US 1910-2000, ylabel Millions contains a line object which displays its values using only markers.

images = [images {img1}];

用图像替换 Content 占位符。

replace(slide3,"Content",Picture(img1));

使用 Comparison 布局将第四张幻灯片添加到演示文稿中。使用此幻灯片显示人口数据的三次和四次外推的比较。

slide4 = add(ppt,"Comparison");
replace(slide4,"Title","Polynomial Degree Changes Extrapolation");

计算总体数据的多项式近似的系数。

n = length(t);
s = (t-1950)/50;
A = zeros(n);
A(:,end) = 1;
for j = n-1:-1:1
    A(:,j) = s .* A(:,j+1);
end
c = A(:,n-3:n)\p;

用文本替换 Left Text 占位符。

replace(slide4,"Left Text","Cubic extrapolation");

计算立方外推。

v = (1910:2020)';
x = (v-1950)/50;
w = (2010-1950)/50;
y = polyval(c,x);
z = polyval(c,w);

fig2 = figure;
hold on
plot(v,y,"k-");
plot(2010,z,"ks");
text(2010,z+15,num2str(z));
hold off

从图中创建一个图像,并将该图像添加到要删除的图像列表中。

img2 = "plot2.png";
saveas(fig2,img2);

Figure contains an axes object. The axes object contains 3 objects of type line, text. One or more of the lines displays its values using only markers

images = [images {img2}];

用图像替换 Left Content 占位符。

replace(slide4,"Left Content",Picture(img2));

用文本替换 Right Text 占位符。

replace(slide4,"Right Text","Quartic extrapolation");

计算四次外推。

c = A(:,n-4:n)\p;
y = polyval(c,x);
z = polyval(c,w);
fig3 = figure;
hold on
plot(v,y,"k-");
plot(2010,z,"ks");
text(2010,z-15,num2str(z));
hold off

从图中创建一个图像,将该图像添加到要删除的图像列表中,并用该图像替换 Right Content 占位符。

img3 = "plot3.png";
saveas(fig3,img3);

Figure contains an axes object. The axes object contains 3 objects of type line, text. One or more of the lines displays its values using only markers

images = [images {img3}];
replace(slide4,"Right Content",Picture(img3));

使用 Title and Content 布局将最后一张幻灯片添加到演示文稿中。

slide5 = add(ppt,"Title and Content");
replace(slide5,"Title",...
    "As the degree increases, the extrapolation " + ...
    "becomes even more erratic");

创建一个图来证明随着次数的增加,外插变得更加不稳定。

fig4 = figure;
cla
plot(t,p,"bo")
hold on
axis([1910 2020 0 400])
colors = hsv(8);
labels = {"data"};
for d = 1:8
   [Q,R] = qr(A(:,n-d:n));
   R = R(1:d+1,:);
   Q = Q(:,1:d+1);
   c = R\(Q'*p);   
   y = polyval(c,x);
   z = polyval(c,11);
   plot(v,y,"color",colors(d,:));
   labels{end+1} = "degree = " + int2str(d);
end
legend(labels, "Location", "NorthWest")
hold off

从图中创建一个图像并用该图像替换 Content 占位符。

img4 = "plot4.png";
saveas(fig4,img4);

Figure contains an axes object. The axes object contains 9 objects of type line. One or more of the lines displays its values using only markers These objects represent data, degree = 1, degree = 2, degree = 3, degree = 4, degree = 5, degree = 6, degree = 7, degree = 8.

images = [images {img4}];
replace(slide5,"Content",Picture(img4));

关闭并查看演示文稿

close(ppt);
rptview(ppt);

删除图像

当演示文稿关闭时,图像会被复制到演示文稿中。现在,您可以删除图像。

len = length(images);
for i = 1:len
    delete(images{i});
end

另请参阅

|

主题