-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveppt.m
120 lines (102 loc) · 3.8 KB
/
saveppt.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
% SAVEPPT saves plots to PowerPoint.
% function SAVEPPT(filespec,title,prnopt) saves the current Matlab figure
% window or Simulink model window to a PowerPoint file designated by
% filespec. If filespec is omitted, the user is prompted to enter
% one via UIPUTFILE. If the path is omitted from filespec, the
% PowerPoint file is created in the current Matlab working directory.
%
% Optional input argument TITLE will add a title to the PowerPoint slide.
%
% Optional input argument PRNOPT is used to specify additional save options:
%
% -fHandle Handle of figure window to save
% -sName Name of Simulink model window to save
% -r### Set bitmap resolution to ### dots/inch (This applies only to large
% images which get converted to BMP when captured by the clipboard.)
%
% Examples:
% >> saveppt
% Prompts user for valid filename and saves current figure
% >> saveppt('junk.ppt')
% Saves current figure to PowerPoint file called junk.ppt
% >> saveppt('junk.ppt','Stock Price','-f3')
% Saves figure #3 to PowerPoint file called junk.ppt with a
% slide title of "Stock Price".
% >> saveppt('models.ppt','System Block Diagram','-sMainBlock')
% Saves Simulink model named "MainBlock" to file called models.ppt
% with a slide title of "System Block Diagram".
%
% The command-line method of invoking SAVEPPT will also work:
% >> saveppt models.ppt 'System Block Diagram' -sMainBlock
%
% However, if you want to save a specific figure or simulink model
% without title text, you must use the function-call method:
% >> saveppt('models.ppt','','-f8')
%Ver 2.2, Copyright 2005, Mark W. Brown, mwbrown@ieee.org
% changed slide type to include title.
% added input parameter for title text.
% added support for int32 and single data types for Matlab 6.0
% added comments about changing bitmap resolution (for large images only)
% swapped order of opening PPT and copying to clipboard (thanks to David Abraham)
% made PPT invisible during save operations (thanks to Noah Siegel)
function saveppt(filespec,titletext,prnopt)
% Establish valid file name:
if nargin<1 | isempty(filespec);
[fname, fpath] = uiputfile('*.ppt');
if fpath == 0; return; end
filespec = fullfile(fpath,fname);
else
[fpath,fname,fext] = fileparts(filespec);
if isempty(fpath); fpath = pwd; end
if isempty(fext); fext = '.ppt'; end
filespec = fullfile(fpath,[fname,fext]);
end
% Default title text:
if nargin<2
titletext = '';
end
% Start an ActiveX session with PowerPoint:
ppt = actxserver('PowerPoint.Application');
% Capture current figure/model into clipboard:
if nargin<3
print -dmeta
else
print('-dmeta',prnopt)
end
if ~exist(filespec,'file');
% Create new presentation:
op = invoke(ppt.Presentations,'Add');
else
% Open existing presentation:
op = invoke(ppt.Presentations,'Open',filespec,[],[],0);
end
% Get current number of slides:
slide_count = get(op.Slides,'Count');
% Add a new slide (with title object):
slide_count = int32(double(slide_count)+1);
new_slide = invoke(op.Slides,'Add',slide_count,11);
% Insert text into the title object:
set(new_slide.Shapes.Title.TextFrame.TextRange,'Text',titletext);
% Get height and width of slide:
slide_H = op.PageSetup.SlideHeight;
slide_W = op.PageSetup.SlideWidth;
% Paste the contents of the Clipboard:
pic1 = invoke(new_slide.Shapes,'Paste');
% Get height and width of picture:
pic_H = get(pic1,'Height');
pic_W = get(pic1,'Width');
% Center picture on page (below title area):
set(pic1,'Left',single((double(slide_W) - double(pic_W))/2));
set(pic1,'Top',single(double(slide_H) - double(pic_H)));
if ~exist(filespec,'file')
% Save file as new:
invoke(op,'SaveAs',filespec,1);
else
% Save existing file:
invoke(op,'Save');
end
% Close the presentation window:
invoke(op,'Close');
% Quit PowerPoint
invoke(ppt,'Quit');
return