MATLAB: How to copy a figure containing into a powerpoint slide created with activex

activexpowerpoint

I am trying to paste a figure that I have copied in my clipboard onto a slide within a for loop and I keep erroring out. Here is my code:
for i=1:20
fh=fig
%code code code
%plot
print(gcf,'-dmeta'); %copy figure to clipboard
%create powerpoint and new slide
ppt = actxserver('PowerPoint.Application');
ppt.Visible = 1;
ppt.Presentations.Add()
layout = ppt.ActivePresentation.SlideMaster.CustomLayouts.Item(2);
ppt.ActivePresentation.Slides.AddSlide(1, layout);
%paste?
ppt.selection.PasteSpecial(0,0,1,0,3) %error, not valid
end

Best Answer

Hi,
As per my understanding you are trying to copy figure to clipboard and paste in PowerPoint slide. Here is a sample code to do this:
% Plot a figure.
fig = figure();
plot(1:10, 2:2:20,'*b');
ppt = actxserver('PowerPoint.Application');
% Assuming there is a PPT (with at least 1 slide) open
activePPT = get(ppt, 'ActivePresentation');
% Slides
slides = activePPT.Slides;
slide = slides.Item(1); % Select a slide
slide.Select;
% Shapes
shapes = slide.Shapes;
shape = shapes.Item(2); % Select an area
shape.Select;
% Copy figure
print(fig, '-clipboard', '-dmeta')
% Paste
pasted_shape = shapes.PasteSpecial(2);