4.81818

4.8 | 34 ratings Rate this file 108 Downloads (last 30 days) File Size: 618 KB File ID: #19322

saveppt2

by

 

25 Mar 2008 (Updated )

Save Matlab figure(s) to a PowerPoint Slide

| Watch this File

File Information
Description

saveppt2 builds on saveppt to include many new options.

Saveppt2 Features:
Save multiple plots to the same PowerPoint slide.
Create slides with and without a title.
Add notes to a PowerPoint slide
Scale the figure to fill the slide (with and without locking aspect ratio).
Horizontally and vertically align figures on the slide.
Use a template file for creating a new presentation. (Useful for automated reports)
Add padding to any of the sides for use with scaling or aligning figures.

New in Jun 2010.
Can choose between Metafile or bitmap drivers when copy and pasting to PowerPoint.
Add a blank title page or blank page from Matlab.
Add comments & textbox to slide.
Make PowerPoint visible during operations. (Required for Comments).
If there is another PowerPoint presentation open, PowerPoint will not quit.
Disable closing of PowerPoint presentation and return ActiveX object for further programmatic slide manipulation.

Also added a method to do batch processing by keeping an ActiveX session open and just adding new slides to it.
ppt=saveppt2('batch.ppt','init');
  for i=1:10
      plot(rand(1,100),rand(1,100),'*');
      saveppt2('ppt',ppt)
end
saveppt2('batch.ppt','ppt',ppt,'close');

The zip file includes saveppt2 and a sample presentation created with saveppt2. The code for creating the presentation is also included.
--
GitHub Project:
http://github.com/jedediahfrey/Jedediah-Frey-s-MATLABCentral-Projects

Acknowledgements

Saveppt, Xlswrite1, and Validate Input inspired this file.

This file inspired Preclinical Pkpd Modeling and Block Edf Heat Map View.

MATLAB release MATLAB 7.5 (R2007b)
Other requirements Requires Windows because it uses ActiveX. This was tested on 2007b and Office 2000 on Windows XP SP3. It should work with other releases.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (53)
16 Sep 2014 Saagar

Is it possible to embed the .fig file onto the ppt slide?

02 May 2014 Dalectric

Excellent script. Very useful.
For those that struggled with the template option, I found you had to specify the full path to the template file for it to work.

10 Feb 2014 Eric Sampson

Hi all, for those of you who are encountering an error when using this function with PowerPoint 2013, to correct this issue you can replace the following line:
pic1 = invoke(new_slide.Shapes,'Paste');

with the following two lines:

picShapeRange = invoke(new_slide.Shapes,'Paste');
pic1 = invoke(picShapeRange,'Item',1);

This fix will still work when using older versions of PowerPoint.

28 Jan 2014 Jedediah Frey

My work machines are stuck at Office 2003 and I use OpenOffice on all my personal machines. I have no way to test this out with newer version of Office. Sorry.

I changed the Git projects to 1 project per repository: https://github.com/jedediahfrey/matlab_saveppt2

If anyone would like to fork it and submit your changes.

27 Jan 2014 Dan

Dale: Thank you for posting your work around. You saved me!

Jedediah: I love this script

23 Jan 2014 Dale

This is great, thanks Jed. I have similar issue as Deng first noted below, seems to be related to Office 2013, because it did work with older version of Office.

A work around is to comment out the section which fits and scales the figure into the slide.

comment out from line approx 560 % Get height and width of picture: to the end of the for i=1:fig.count loop.

Thanks

09 Jan 2014 Sue Peters

I had a similar problem to Deng, and wondering if anyone has found a solution. Thanks

09 Jan 2014 Sue Peters  
07 Nov 2013 Xionghua

This is great and I use saveppt old version in the past. One question: On different computer, it generates different slides. I forget where to change the setting to avoid that. Can anyone give me a hint?

31 Jul 2013 Matthew

I love this code, thank you! It's incredibly useful for automatically creating a PPT presentation from a large number of plots in my code.

But, I ran into a puzzling error where it refused to insert a figure into a PPT file, for a particular figure I created in GUIDE. It turned out the problem was the "num2str" call on line 557 was truncating digits in the figure handle value and the result was not a valid figure handle. I changed the code on line 557 to "num2str(addlParms.figure(i),'%.20f')" and now it seems to work!

29 Jul 2013 Ankit Gupta

Is it possible to use this for multiple images already in a folder, or matlab array, or use it in a loop adding new image in new slide.

28 Jun 2013 Nevzus

Is there a way to make this work on Mac? Thanks for your suggestions.

26 Jun 2013 vic tsai

great

18 Jun 2013 Deng

Hi Jedediah
Recently I use this file to export .fig to office 2013. I found an error like this :
Error using Interface.Microsoft_PowerPoint_15.0_Object_Library.ShapeRange/get
Error: Object returned error code: 0x800A01A8

Error in saveppt2 (line 561)
pic_H = get(pic1,'Height');
What should I do?

Thanks for your help

17 Jun 2013 Linda Seltzer

Please disregard my previous question. I found the error in my script. I shouldn't be debugging and posting questions at 5 am, but alas it happens.

17 Jun 2013 Linda Seltzer

I tried to run the example in the documentation:
happt = saveppt2('batch.ppt','init');
for i = 1:10
plot(rand(1,100),rand(1,100),'*');
saveppt2('ppt',happt)
end
saveppt2('batch.ppt','ppt','happt','close');

I am getting the following error message.
??? Error using ==> saveppt2 at 247
Failed invoking object. Verify that it is a PPT object as returned from a
saveppt2(file,'init') function.

Error in ==> testbatchppt at 6
saveppt2('batch.ppt','ppt','happt','close');

We are using
MATLAB Version 7.12.0.635 (R2011a)
Windows Vista Business Version 6.0

Thank you for any information / fixes.

08 Jun 2013 Tai-Yen Chen

just wonder is there a way to change the title font size??

08 Jun 2013 Tai-Yen Chen  
15 Nov 2012 Sachin  
21 May 2012 Naira  
11 May 2012 Rohit Mundra

Works really well on XP however it crashes MATLAB on my Windows 7 system (tried both, batch mode and otherwise) - I suspect it has something to do with the ActiveX controls...

29 Mar 2012 Sachin

Excellent tool.. really helps me a lot.

Is there any way, we can make this tool through a compiled Matlab code?

12 Jan 2012 Craig Remillard

@Manouch - I also get this error. It happens when I change the renderer from '' or 'painters' to 'opengl' or 'zbuffer':

It does not error out if I change -dmeta to -dbitmap in this line:

print('-dmeta',['-f' num2str(addlParms.figure(i))],rendOpt,resOpt);

BUT, then I have two other problems - first, the figures all become visible even though I have set them as not-visible (I assume there's a screen capture in bitmap print driver, because this happens when I just export figures to bitmap in Matlab as well). Second, all the bitmap figures in the PPT are solid black.

To summarize: the raster renderers crash out if I don't use -dbitmap, and they output black pictures and fill my screen if I don't. And the vector renderer (painters) doesn't crash with either option, but the vector images are insanely large for my dataset with -dmeta, and the raster images are still black blocks with -dbitmap. Can anyone offer a way to export raster images? I'll keep working on it and let y'all know if I figure it out.

27 Dec 2011 Hugh

Very useful already. Wonderful!

One revision suggestion. The chart in Matlab with date labels looking nice, but when exported to powerpoint, the date lables are crowded together. The resizing did not preserve the original look.

14 Dec 2011 Monica Barbu-McInnis

Awesome tool! I have a question about the 'template' keyword. I am specifying a template when creating a new ppt file of two figures, but the figures end up placed on a new slide while the first slide with the actual ppt template is blank. Am I missing any additional keywords?

31 Oct 2011 Carey Smith

Works nice & simple

16 Sep 2011 Nando Trindade

Awesome script! Is there a way to put an option to add a border around the figure? We had a different script that didn't work too well, but it had a line ppt_pic=op.Application.Windows.Item(1).Selection.ShapeRange.Item(1);
ppt_pic.Line.Visible = -1; I'm not sure how to replace the ppt_pic to use your code to add this border.

Also, is there a way to place the title in the template title slot instead of a position above the figure? Thanks for sharing.

15 Jul 2011 Jedediah Frey

Other requirements: Requires Windows because it uses ActiveX. This was tested on 2007b and Office 2000 on Windows XP SP3. It should work with other releases.

11 Jul 2011 Warwick

Attempting to run the first example, I get 'Error opening PowerPoint'. I'm using a Mac (R2011a and MS Office for Mac 2011 - is it likely this is the problem?

25 Apr 2011 Lan Du

powerful!

09 Mar 2011 Jonathan

Works great - very helpful tool!

04 Mar 2011 Andrew Wind

Just found this function following the Pick of the Week review of 2004 picks. It appears to work great thus far for figures. Is there any way to capture a GUI though? I can't seem to point it to the GUI even if all my other figures are closed. I would love to capture my Input GUI with all the values and then the results figures all in one ppt.

22 Dec 2010 Jedediah Frey

Unfortunately my company is sticking with Office 2000 for the near future and I only run OS X/Linux at home.

I will try and get to a computer with the latest office and see if I can fix that.

14 Dec 2010 Henik Vargas

Thank you for such an excellent and well coded function. Test automation in matlab would be incomplete without it. One minor question: Is it possible to save the file as pptx instead of ppt?

30 Sep 2010 Jedediah Frey

How are you calling the template? PowerPoint is complaining that it can't open the template file that you provide it.

09 Sep 2010 Danielle

I've used your script for several years now--it's great!

However, when trying to use the template argument, I get the following error:

322 op = invoke(ppt.Presentations,'Open',addlParms.template,[],[],0);
??? Invoke Error, Dispatch Exception:
Source: Microsoft Office PowerPoint 2007
Description: PowerPoint could not open the file.
Help File:
Help Context ID: 0

This has been a problem for both the v1 and v2 scripts. Any advice would be appreciated!

13 Aug 2010 Jedediah Frey

I'm only on Windows XP with Office 2000 and get no problems with surf or plot3 So it sounds like something with your setup.

Have you tried using the bitmap driver?

26 Jul 2010 Manouch

Unfortunately it doesn't work for me. But I think, it is my lack of knowledge about Matlab...
I tried to save a 3d-figure created with surf in a powerpoint file with saveppt2 an got the following message:

??? Invoke Error, Dispatch Exception:
Source: Microsoft Office PowerPoint 2007
Description: Shapes.Paste : Invalid request. Clipboard is empty or contains data which may not be pasted here.
Help File:
Help Context ID: 0

Error in ==> saveppt2 at 559
pic1 = invoke(new_slide.Shapes,'Paste');

I would appreciate, if anyone could tell me, what was my mistake.

Thanks in advance!

15 Mar 2010 Fergus

Precious time saver!

29 Jan 2010 john

Thanks for the great tool! I added the following code that lets you add text to a slide. (Not showing the argument parsing part) Someone with more experience with this can surely improve it.

% Add textbox if they are specified
if isfield(addlParms,'textbox')

% Convert \n and \t into characters for powerpoint
warning('off','MATLAB:strrep:InvalidInputType');
addlParms.textbox=strrep(addlParms.textbox,'\n',13);
addlParms.textbox=strrep(addlParms.textbox,'\t',9);

% Set layout of slide
new_slide.Layout='ppLayoutText';

% Set some handles
% I'm not sure what's different between TextFrame1 and TextFrame2
% But, the AutoSize property isn't available on TF1 (and it doesn't
% seem to matter if I add the actual text to TF1 or TF2.
tf1 = new_slide.Shapes.Placeholders(1).Item(2).TextFrame;
tf2 = new_slide.Shapes.Placeholders(1).Item(2).TextFrame2;

% Turn off bullets
tf1.TextRange.ParagraphFormat.Bullet.Type='ppBulletNone';

% Set text contents and font
tf2.TextRange.Font.Name = 'Courier New';

% Format paragraph, remove spaces between lines
pf = tf2.TextRange.ParagraphFormat;
set( pf, 'SpaceBefore', 0.2, ...
'SpaceWithin', 1, ...
'SpaceAfter', 0);

% Format shape so the text will be shrunken to fit in box
tf2.AutoSize='msoAutoSizeTextToFitShape';

% Finally, add the text
tf2.TextRange.Text = addlParms.textbox;

% I have no idea why I have to do this, if I don't the
% text does not resize.
ppt.visible=1;
op.NewWindow;
pause(2)
%fprintf('Autosize property is: %s\n', get(tf2, 'AutoSize'));
end

27 Jan 2010 Björn

Great great Tool,

have been using your ver. 1 for some time and just switched over to ver. 2. Short question though, do you have a script to easily export tables (or present matrices/cell arrays) from Matlab to ppt? I have been looking for a script for some time, but cannot find a really useful one...

Regards

11 Nov 2009 Paul

To avoid losing unsaved ppts, comment out this line:

invoke(ppt,'Quit');

(line 420 in 25 Mar 2008 version)

If you don't have any open ppt's, powerpoint will still close normally

[Tested using powerpoint ver 2007]

11 Nov 2009 Paul  
04 Nov 2009 Dan Selters  
20 May 2009 Jaroslaw Tuszynski

great improvement over saveppt . Thanks

18 Apr 2009 jhh

Works great.
One suggestion: you use
print('-dmeta'...;
to keep a figure's size unchanged,
print('-dbitmap',['-f' ...
should be used.
Perhaps add this as an extra option?

20 Mar 2009 Ameya Deoras

Saved me some time. Works with R2009a

14 Nov 2008 Wolfgang Schwanghart

Great function. The only minus is, that it automatically shuts down powerpoint and there is no warning. So unsaved ppts will be lost.

03 Nov 2008 Vincent Bonin  
23 Jun 2008 c garven

Thanks

01 Apr 2008 Nathan Brahms

Very nice! This will make my life easier...

28 Mar 2008 James Cai

Excellent

28 Mar 2008 D Selters

Outstanding!

Updates
24 Mar 2010

Added github repository.

03 Jun 2010

Update to version 3.5.
-
Incorporated suggestions in comments. In addition to code cleanup, minor bugs, 'workarounds' for ActiveX odd behavior.

Contact us