- 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.
Why images are displayed twice when using the publish function in a word
59 views (last 30 days)
Show older comments
Jeremie AUZOUX
on 5 Nov 2021
Answered: George Papazafeiropoulos
on 25 Oct 2023
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 !
0 Comments
Accepted Answer
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:
0 Comments
More Answers (1)
George Papazafeiropoulos
on 25 Oct 2023
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.
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks 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!