How to save all my matlab plots from for loop?

51 visualizaciones (últimos 30 días)
Robert  Flores
Robert Flores el 23 de Sept. de 2019
Comentada: Walter Roberson el 23 de Sept. de 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 comentarios
Robert  Flores
Robert Flores el 23 de Sept. de 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 el 23 de Sept. de 2019
h = gcf;
ax = gca;
should have been
fig = gcf;
ax = gca(fig);

Iniciar sesión para comentar.

Respuesta aceptada

Jon
Jon el 23 de Sept. de 2019
Editada: Jon el 23 de Sept. de 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 comentarios
Robert  Flores
Robert Flores el 23 de Sept. de 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 el 23 de Sept. de 2019
Editada: Jon el 23 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by