How to save all my matlab plots from for loop?

49 Ansichten (letzte 30 Tage)
Robert  Flores
Robert Flores am 23 Sep. 2019
Kommentiert: Walter Roberson am 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 Kommentare
Robert  Flores
Robert Flores am 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 am 23 Sep. 2019
h = gcf;
ax = gca;
should have been
fig = gcf;
ax = gca(fig);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jon
Jon am 23 Sep. 2019
Bearbeitet: Jon am 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 Kommentare
Robert  Flores
Robert Flores am 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 am 23 Sep. 2019
Bearbeitet: Jon am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by