www.gusucode.com > rptgen 案例源码程序 matlab代码 > rptgen/pptpop.m

    function pptpop( varargin )
%PPTPOP PPT-based report example
%
%   This example is based on the MATLAB example: "Predicting the US
%   Population." That example was generated using MATLAB Publish. This
%   example is generated using the PPT API. This example is intended to
%   illustrate a simple use of the API.
%
%   pptpop() generates a MS PowerPoint presentation based on the default
%   template.
% 
%   pptpop(template) generates a MS PowerPoint presentation based on the
%   specified empty presentation as a template. the template must be
%   created from the default template to honor the slide layout names and 
%   placeholder names.

%   Copyright MathWorks, 2015.

% This statement eliminates the need to qualify the names of PPT
% objects in this function, e.g., you can refer to 
% mlreportgen.ppt.Presentation simply as Presentation.
import mlreportgen.ppt.*;

% Create a cell array to hold images generate for this presentation. 
% That will facilitate deleting the images at the end of presentation
% generation when they are no longer needed.
images = {};

%% Create a presentation.
if nargin < 1
    % Use the default template
    pre = Presentation('population');
else
    % Use the 
    pre = Presentation('population', varargin{1});
end

%% Add the first slide to the presentation, based on the Title Slide layout.
% In general, PowerPoint presentations are structured in slides created 
% from predefined layouts that contains place holders which users will fill 
% in with generated content. The predefined layouts belong to a template 
% slide master that defines the styles.
slide = add(pre, 'Title Slide');

% Replace the title and subtitle in the slide, using the replace method 
% providing the placeholder name
replace(slide, 'Title', 'Modeling the US Population');
replace(slide, 'Subtitle', 'A Risky Business');


%% Add the second slide to the presentation based on Title and Content layout.
slide = add(pre, 'Title and Content');
replace(slide, 'Title', 'Population Modeling Approach');
% Add some bullets text to the Content placeholder, using a cell array
replace(slide, '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 based on Title and Content layout.
slide = add(pre, 'Title and Content');
replace(slide, 'Title', 'US Census data from 1900 to 2000');

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

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

% Plot
plot(t,p,'bo');
axis([1900 2020 0 400]);
title('Population of the U.S. 1900-2000');
ylabel('Millions');

img = printPlot('plot1');

% Replace the Content placeholder by the plot
replace(slide, 'Content', Picture(img));

% Add plot image to the list of images to be deleted at the end of
% presentation generation. Note that you need to keep the images around until
% the presentation is closed. That is because you cannot add images to the
% presentation package while the presentation file is still open.
images = [images {img}]; %#ok<*NASGU>

%% Add the fourth slide to the presentation based on Comparison layout.
slide = add(pre, 'Comparison');
replace(slide, 'Title', 'Polynomial Degree Changes Extrapolation');

% Compute coefficients for a polynomial approximation to 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 Left Text and Left Content.
replace(slide, 'Left Text', 'Cubic extrapolation');

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

hold on
plot(v,y,'k-');
plot(2010,z,'ks');
text(2010,z+15,num2str(z));
hold off

img = printPlot('plot2');
replace(slide, 'Left Content', Picture(img));
images = [images {img}]; %#ok<*NASGU>


% Replace Right Text and Right Content.
replace(slide, 'Right Text', 'Quartic extrapolation');

c = A(:,n-4:n)\p;
y = polyval(c,x);
z = polyval(c,w);

hold on
plot(v,y,'k-');
plot(2010,z,'ks');
text(2010,z-15,num2str(z));
hold off

img = printPlot('plot3');
replace(slide, 'Right Content', Picture(img));
images = [images {img}]; %#ok<*NASGU>

%% Add the last slide to the presentation based on Title and Content layout.
slide = add(pre, 'Title and Content');
replace(slide, 'Title', 'As the degree increases, the extrapolation becomes even more erratic');

cla
plot(t,p,'bo'); hold on; axis([1900 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);    % Same as c = A(:,n-d:n)\p;
   y = polyval(c,x);
   z = polyval(c,11);
   plot(v,y,'color',colors(d,:));
   labels{end+1} = ['degree = ' int2str(d)]; %#ok<AGROW>
end
legend(labels, 'Location', 'NorthWest');

img = printPlot('plot4');
replace(slide, 'Content', Picture(img));
images = [images {img}]; %#ok<*NASGU>

%% Finally, close the presentation and open it in Windows
close(pre);
if ispc
    winopen(pre.OutputPath);
end

% Closing the presentation causes the images needed for the
% presentation to be copied into it. So we can now delete them.
for i = 1:length(images)
    delete(images{i});
end

end

function imgname = printPlot(name)
% Convert the specified plot to an image.
% Return the image name so it can be deleted at the
% end of presentation generation.
import mlreportgen.ppt.*;

% Select an appropriate image type, depending
% on the platform.
if ~ispc
    imgtype = '-dpng';
    imgname= [name '.png'];
else
    % This Microsoft-specific vector graphics format
    % can yield better quality images in Word documents.
    imgtype = '-dmeta';
    imgname = [name '.emf'];
end

% Convert figure to the specified image type.
print(imgtype, imgname);

% Delete plot figure window.
delete(gcf);

end