MATLAB Answers

How to save all my matlab plots from for loop?

95 views (last 30 days)
Robert  Flores
Robert Flores on 23 Sep 2019
Commented: Walter Roberson on 23 Sep 2019
Hello Everyone,
I am trying to save all my plots from my for loop into a word document. I have tried using save2word, but that only saves the last figure from the for loop. If anyone can give me some insight on what I can add to my script, such that I am able to export all plots from MATLAB into a Word doc. It will be most appreciated.
Also, I tried using the print function, and that didn't work for me yet either.
Sincerely,
Robert
CODE:
fName = 'SHOCK2019Graphs.doc'
if exist(fName,'file')
delete(fName)
end
wordHand = save2Word(fName);
for q = 1:16
figure(q)
plot(SBOB{1}.tt,SBOB{1}.Acc(:,q))
hold on
plot(SBridge{5}.tt,SBridge{5}.Acc(:,q))
title(SBOB{1}.labels(q)),xlabel('Time, Measured in Seconds'), ylabel('Acceleration, Measured in ft/sec^2')
legend('Bridge on Box','Bridge','location','northeast')
filename = 'Test.pdf';
print(figure(q),'-dpdf',filename);
end
save2Word(fName,wordHand)

  3 Comments

Walter Roberson
Walter Roberson on 23 Sep 2019
Perhaps
fName = 'SHOCK2019Graphs.doc'
if exist(fName,'file')
delete(fName)
end
wordHand = save2Word(fName);
h = gcf;
ax = gca;
for q = 1:16
%figure(q)
plot(ax, SBOB{1}.tt,SBOB{1}.Acc(:,q))
hold(ax, 'on')
plot(ax, SBridge{5}.tt,SBridge{5}.Acc(:,q))
hold(ax, 'off')
title(ax, SBOB{1}.labels(q)),xlabel('Time, Measured in Seconds'), ylabel('Acceleration, Measured in ft/sec^2')
legend(ax, {'Bridge on Box','Bridge'},'location','northeast')
filename = 'Test.pdf';
print(fig,'-dpdf',filename);
save2Word(fName, filename);
end
Robert  Flores
Robert Flores on 23 Sep 2019
Hello Mr. Robertson,
Unfortunately, the changes you made to the script do not work. I get the following errors:
i) Dot indexing is not supported for variables of this type.
ii) Error in save2Word (line 150)
end_of_doc = get(wordHand.word.activedocument.content,'end');
iii) Error in Drop_Table_SRSs_TimeHists (line 153)
save2Word(fName, filename);
Also, the code would not run because the varible fig was not assigned. Therefore, I suppressed the lines 'filename = 'Test.pdf';' and 'print(fig,'-dpdf',filename);'. I hope this information helps.
Walter Roberson
Walter Roberson on 23 Sep 2019
h = gcf;
ax = gca;
should have been
fig = gcf;
ax = gca(fig);

Sign in to comment.

Accepted Answer

Jon
Jon on 23 Sep 2019
Edited: Jon on 23 Sep 2019
I would recommend using the standard MATLAB function saveas (type doc saveas on the command line for help)
Specifically inside of your for loop right after you make your plot insert something like
filename = ['plot',num2str(q),'.emf'] % plot names plot1.emf, plot2.emf ...
saveas(gcf,filename,'meta') % save as .emf file
In the above I saved as .emf file. I find these work well for including into word but you could use other formats instead.
Note once you have these figure files you will have to manually insert them into your word document.

  2 Comments

Robert  Flores
Robert Flores on 23 Sep 2019
Jon,
Thank you for the script, the code runs. It is unfortunate that I am not able to immediately insert these plots into a word document. But, I can do the manual inerting of the plots in Word, thanks again.
Robert
Jon
Jon on 23 Sep 2019
Glad that worked for you. I'm not sure exactly what your workflow looks like, but I find that I usually want to insert the figures at specific locations within my Word document within some section of explanatory text. So I usually end up pulling them in one at a time anyhow as I get to them in the write up.

Sign in to comment.

More Answers (0)


Translated by