How do I get my plots and "disp" script to show up after its respective code script has concluded?

6 views (last 30 days)
bobby
bobby on 22 Oct 2021
Answered: Dave B on 23 Oct 2021
Hello everyone! So I have a script code that I have to publish, it has about 100 lines of code with about 30 lines of local functions at the end of it. Within the 100 lines, there are multiple section breaks "%%" to seperate each "part" of the question I answered as a means for me to organize.
When publishing as a PDF, HTML, DOC, etc., I would like for the respective plots and disp text to appear immediately underneath the code section it belongs to within the same section
However in reality, the plots and disp text ALWAYS shows up at the very end of the PDF, right underneath the local functions section. I am unsure why my publishing option is doing this. I am using the %% correctly to indicate section breaks, AND I am using snapnow at the correct places to force MATLAB to publish the images.
I included a visual demonstration below of my issue (Purely for demonstrative purposes, the code itself is not supposed to work but to rather show the overall structure ).
%% Question 1 section
x = [0:.1:100]
y = 2*x
plot(x, y, '-g')
snapnow
disp('Yes this is correct')
%When published, I would want the plot and disp text for this section to appear HERE,
%right before the question 2 section
%% Question 2 section
x2 = [0:.1:100]
y2 = 2*x2
plot(x2, y2, '-b')
snapnow
disp('Yes this is also correct')
%When published, I would want the plot and disp text for this section to appear HERE,
%right before the question 3 section
%% Question 3 section
x3 = [0:.1:100]
y3 = 2*x3
plot(x3, y3, '-r')
snapnow
disp('Yes this is also also correct')
%When published, I would want the plot and disp text for this section to appear HERE,
%right before the functions section
%% Function section
answer = function pretend(this, is, works)
random = stuff + text
imagine = this + works
end
%In reality, publishing has all of the 3 section's (Question 1, Question 2, Question 3)
%plots and disp text appears here at the very end of the published file.
The issue persists on multiple devices (windows desktop and apple laptop) on multiple versions I used (MATLAB 2021a & MATLAB2021b,). Thank you in advance to all who contribute!

Answers (1)

Dave B
Dave B on 23 Oct 2021
What you describe that you want is the behavior I'd expect. I ran your code and got exactly this, but I made a few changes:
  • I added semi-colons to the assignments (because otherwise there was a ton of output)
  • I adjusted the syntax error: function answer = pretend(this, is, works) (without that it can't run so can't publish)
Did you experiment with your demo code? I wonder if we could narrow this down to something about your configuration vs. the specific code.
(The attached was run in 2020b, but I got the same result in 2021a)

Community Treasure Hunt

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

Start Hunting!

Translated by