Why images are displayed twice when using the publish function in a word

33 views (last 30 days)
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

Richard Quist
Richard Quist on 23 Nov 2021
The publish command captures figures that it thinks have changed, so something has happened to figure 1 when the "Test 2" section is being processed. Assuming that function2 isn't doing something to change figure 1, you could try the following:
  • Add a call to drawnow after the clearvars call; this will give MATLAB a chance to make sure any pending changes to figure 1 have been processed.
  • It may help to move the mouse to a corner of your screen before running your code that creates/publishes the figures because sometimes the mouse can trigger changes. For example, in recent versions of MATLAB the axes toolbar will appear if the mouse is positioned over the axes and this may cause publish to think the figure has changed.
  • if using R2018b or later, another way that can help prevent the mouse from interfering is to disable the default interactions on the axes in your scripts that generate the figures. See disableDefaultInteractivity in the MATLAB documentation.

More Answers (0)


Find more on Graphics Object Identification 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!