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

조회 수: 60 (최근 30일)
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 !

채택된 답변

Richard Quist
Richard Quist 2021년 11월 23일
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.

추가 답변 (1개)

George Papazafeiropoulos
George Papazafeiropoulos 2023년 10월 25일
Some other fixes, if the answer by Richard Quist does not work:
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.


Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by