Why images are displayed twice when using the publish function in a word
Show older comments
I post-process the results and create an automatic report in word with the MATLAB Publish function. This is the publish code :
options_doc_nocode.format = 'doc'; %word file
options_doc_nocode.showCode = false; %I don't want to see the code in the word file
options_doc_nocode.outputDir = path; %current path
publish('wordpublish.m',options_doc_nocode); %execute the publish function (described below)
winopen(wordpublish.doc); %I open the word file once written
And this is the wordpublish.m code :
%% *Test 1*
function1 %I plot only one figure called figure 1
clearvars -except 'myvars'
%% *Test 2*
function2 %I plot only one figure called figure 2
Finaly, in the word file, the figure 1 appears two times in the two subsections and I don't understand why.

I am sure that the function "function1" plots only once one figure. I also tried to publish in a .pdf file but there is the same problem.
Thanks !
Accepted Answer
More Answers (1)
George Papazafeiropoulos
on 25 Oct 2023
0 votes
1) Use figure() instead of figure(i) (where i is the figure number) in the code that is published.
2) Add a call to pause() after the drawnow call. Maybe a small amount of time could be specified in pause() function, to give certain delay for the compiler to complete the operation of writing the png file before starting the processing of the next code segment.
Categories
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!