main content

generate a presentation from the results of a matlab application -凯发k8网页登录

this example shows how to use the matlab® api for powerpoint® (ppt api) to generate a microsoft® powerpoint® presentation from the results of a matlab application. the example generates a presentation from the results of an application that predicts the united states population. here are the slides that the example generates:

create the presentation

import the ppt package so that you do not have to use long, fully qualified names.

import mlreportgen.ppt.*;

to facilitate deleting the images that the example generates, create a cell array to hold the images.

images = {};

create a presentation, using the default template.

ppt = presentation('population.pptx');
open(ppt);

add slides to the presentation

powerpoint presentations consist of slides that are created from predefined layouts. the layouts contain placeholders that you fill with generated content. the predefined layouts belong to a template slide master that defines the styles.

add the first slide to the presentation, using the title slide layout.

slide1 = add(ppt,'title slide');

replace the title and subtitle in the slide by using the replace method.

replace(slide1,'title','modeling the us population');
replace(slide1,'subtitle','a risky business');

add the second slide to the presentation using the title and content layout. replace the title.

slide2 = add(ppt,'title and content');
replace(slide2,'title','population modeling approach');

add text to the content placeholder using a cell array.

replace(slide2,'content',{ ...
    'fit polynomial to u.s. 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'});

add the third slide to the presentation using the title and content layout. replace the title.

slide3 = add(ppt,'title and content');
replace(slide3,'title','us census data from 1900 to 2000');

create a plot of us census data from 1910 to 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 u.s. 1910-2000');
ylabel('millions');

convert the plot to an image. add the image to the list of images to be deleted at the end of presentation generation. you must not delete the images until after you close the presentation.

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

images = [images {img1}];

replace the content placeholder with the image.

replace(slide3,'content',picture(img1));

add the fourth slide to the presentation using the comparison layout. use this slide to show a comparison of the cubic and quartic extrapolations of the population data.

slide4 = add(ppt,'comparison');
replace(slide4,'title','polynomial degree changes extrapolation');

compute the coefficients for a polynomial approximation of the population data.

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;

replace the left text placeholder with text.

replace(slide4,'left text','cubic extrapolation');

compute the 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

create an image from the plot and add the image to the list of images to be deleted.

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

images = [images {img2}];

replace the left content placeholder with the image.

replace(slide4,'left content',picture(img2));

replace the right text placeholder with text.

replace(slide4,'right text','quartic extrapolation');

compute the 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

create an image from the plot, add the image to the list of images to be deleted, and replace the right content placeholder with the image.

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

images = [images {img3}];
replace(slide4,'right content',picture(img3));

add the last slide to the presentation using the title and content layout.

slide5 = add(ppt,'title and content');
replace(slide5,'title','as the degree increases, the extrapolation becomes even more erratic');

create a plot to demonstrate that 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

create an image from the plot and replace the content placeholder with the image.

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

images = [images {img4}];
replace(slide5,'content',picture(img4));

close and view the presentation

close(ppt);
rptview(ppt);

delete the images

when the presentation closes, the images are copied into the presentation. now, you can delete the images.

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

see also

|

related topics

    网站地图