For Loop iteration only displays last plot in PPT slides

I created a code to display my plots onto a Powerpoint (PPT) slide. The issue lies with MATLAB would generate all the plots but would only display the last plot on all the PPT slides. So far the only method is to copy the for loop, change the range (ex: 1:1, 2:2, 3:3, etc) and the "plot.png" numerical. I want to eliminate this tedious method and have an easier method for each slide to display each corresponding plot.
In this example length(fileList) = 6. The fileList respresents the 6 plots generated in this example. Prior I had length(fileList) in the for loop but it would only display the last plot on the slides.
X1 = length(fileList);
for j = 1:1 %length(fileList)
imgFIG = openfig(strcat(fileList(j).folder,'\',fileList(j).name));
saveas(imgFIG,'plot.png');
imgPNG = imread('plot.png');
imgPNG2 = imresize(imgPNG, 1.5);
imwrite(imgPNG2,'plot2.png')
imgPNG3 = Picture('plot2.png');
fName = fileList(j).name;
z = fName(27:28);
dataSlides = add(ppt,'Title and Content');
newTitle = strcat('Step', {' '}, z,' Simulations');
replace(dataSlides,'Title', newTitle);
replace(dataSlides,'Content', imgPNG3);
clearvars -except j pL ppt directory fileList dirSize X1
end
for j = 2:1:X1
imgFIG = openfig(strcat(fileList(j).folder,'\',fileList(j).name));
saveas(imgFIG,'plot4.png');
imgPNG = imread('plot4.png');
imgPNG2 = imresize(imgPNG, 1.5);
imwrite(imgPNG2,'plot5.png')
imgPNG3 = Picture('plot5.png');
fName = fileList(j).name;
z = fName(27:28);
dataSlides = add(ppt,'Title and Content');
newTitle = strcat('Step', {' '}, z,' Simulations');
replace(dataSlides,'Title', newTitle);
replace(dataSlides,'Content', imgPNG3);
clearvars -except j pL ppt directory fileList dirSize X1
end
close(ppt);
rptview(ppt);
end

8 Comments

Would that method remove the use of a For Loop?
I’m trying to condense it down so that it’s not tedious. I’ll be using this code for other examples that’ll have a much greater fileList value than 6.
From what I see in that existing tool i’d need to create functions for all the slides? I’d like to use the code i have now but just make the necessary tweaks
ok, I understand it seems easier to work on your code , but I cannot beacuse I don't have the Report Gen Toolbox
otherwise I just tested a simplified example based on this other submission, and it seems to be pretty effective (I suppose it's not gonna be very difficult to adapt to your own needs)
I see it has been downloaded > 9k times (wow !)
%
% Example 1: usage of exportToPPTX with blank presentation
%
%% Start new presentation
pptx = exportToPPTX('', ...
'Dimensions',[12 6], ...
'Title','Example Presentation', ...
'Author','MATLAB', ...
'Subject','Automatically generated PPTX file', ...
'Comments','This file has been automatically generated by exportToPPTX');
% Another way of setting some properties
pptx.title = 'Demonstration Presentation';
pptx.author = 'exportToPPTX Example';
pptx.subject = 'Demonstration of various exportToPPTX commands';
pptx.description = 'Description goes in here';
% Additionally background color for all slides can be set as follows:
% exportToPPTX('new','BackgroundColor',[0.5 0.5 0.5]);
%% Add some slides
figH = figure('Renderer','zbuffer'); mesh(peaks); view(0,0);
for islide=1:5,
slideId = pptx.addSlide();
fprintf('Added slide %d\n',slideId);
pptx.addPicture(figH);
pptx.addTextbox(sprintf('Slide Number %d',slideId));
pptx.addNote(sprintf('Notes data: slide number %d',slideId));
% Rotate mesh on each slide
view(18*islide,18*islide);
end
close(figH);
%% Check current presentation
fprintf('Presentation size: %f x %f\n',pptx.dimensions);
fprintf('Number of slides: %d\n',pptx.numSlides);
%% Save presentation and close presentation -- overwrite file if it already exists
% Filename automatically checked for proper extension
newFile = pptx.save('example');
clear pptx
How do you acquire the Report Gen Toolbox and what does it do?
Also with your code I don’t see the correlation with mine. I’m somewhat confused
your provided code example call functions that belong to the ReportGenerator Toolbox (I don't have, but you should have otherwise it would not work at all)
@Mathieu NOE Are you asking for the part 1 of 2 code that corresponds with this code? If so then i can provide
hello again
seems we are now both confused here .
1/ yes my code has nothing to do with yours , it's just an example how to use the suggested FEX submission to do the job - in my eyes, with little work on your side
2/ no , I am not asking for more code from your side , because I cannot help you there , as I cannot run it (I don't have the ReportGenerator Toolbox)
Lol yea i think there was just misunderstanding.
  1. You think this is the only effective method to resolving my issue of the for loop only plotting the last generated plot on all the PPT slides?
  2. I'm looking at the exportToPPTX. Is there a way to add iteration so that i don't have to do "pptx.addSlide('Layout','Two Content');" continuously? I have other examples where I'll have to create 20+ slides.

Sign in to comment.

Answers (1)

Try using
drawnow
after view. It may just do the trick.

4 Comments

Hi. I was able to resolve my issue of the same image showing up on every slide. Lines 3 & 4 fixed this issue. My new problem is how to resize my images. I tried modifying the imresize command with the new imgPNG but no result. Any assistance? @Constantino Carlos Reyes-Aldasoro @Mathieu NOE
for j = 1:a
imgFIG = openfig(strcat(fileList(j).folder,'\',fileList(j).name));
saveas(imgFIG,['plot' num2str(j) '.png']);
imgPNG = Picture(['plot' num2str(j) '.png']);
%imgPNG = imread('plot.png');
%imgPNG2 = imresize(imgPNG, 1.5);
%imwrite(imgPNG2,'plot2.png')
%imgPNG3 = Picture('plot2.png');
fName = fileList(j).name;
z = fName(27:28);
dataSlides = add(ppt,'Title and Content');
newTitle = strcat('Step', {' '}, z,' Simulations');
replace(dataSlides,'Title', newTitle);
replace(dataSlides,'Content', imgPNG);
end
sorry again not to be able to help you in your code - again because I don't have the required toolbox
Not sure I understand what the problem is, is it that the figure is not the correct size inside powerpoint? Or that the image is not exported with the correct dimensions?
@Constantino Carlos Reyes-Aldasoro I'm just trying to implement a resizing command for the plots. Thy're just a little small on the PPT slides and I want to make them a little larger for easier visualization.

Sign in to comment.

Categories

Asked:

on 29 Nov 2023

Commented:

on 8 Dec 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!