Publishing result order when using functions

38 views (last 30 days)
Lowell Toms
Lowell Toms on 26 Dec 2012
Commented: Jingze Li on 10 Feb 2023
Here is my published result:
--------------------------------------------------------
function [] = main(a,b)
disp(['adding ',num2str(a),' + ',num2str(b),' gives: '])
q=add(a,b);
disp(['a result of ',num2str(q)])
end % end main
adding 4 + 10 gives:
function [z] = add(x,y)
z=x+y;
end % end add
a result of 14
--------------------------------------------------------
Why does the "a result of 14" show up at the end and not under function main? Is there anyway to change this odd behavior?

Answers (2)

Roman Kogan
Roman Kogan on 5 Nov 2015
Workaround to Matlab bug #496201
I have found the following workaround to the bug #496201 (which, as of 2015, has been unfixed for 10 years!).
If your sub-functions don't call other sub-functions, then the call to the first sub-function will publish incorrectly.
Otherwise, the first sub-function that calls other sub-functions will be published incorrectly, but others seem to be publishing in the right order.
Workaround: include a dummy sub-function that calls my sub-function, and publish it before calls to the sub-functions that I want to publish.
Example 1: the following publishes incorrectly:
function matlab_bug()
%%The answer to everything is...
my_subfunction();
end
function my_subfunction()
disp('42');
end
..but the following will publish alright:
function matlab_bug()
workaround()
%%The answer to everything is...
my_subfunction();
end
function workaround()
end
function my_subfunction()
disp('42');
end
Example 2: the following code publishes incorrectly:
%%Main
function matlab_bug()
not_a_workaround()
%%The answer to everything is...
my_subfunction();
end
%%Functions
function [val]=my_mult(a, b)
val = a * b;
end
%%Tests
function not_a_workaround()
end
function my_subfunction()
a = my_mult(7,6);
disp(a);
end
...but the following will publish as intended:
%%Main
function matlab_bug()
workaround()
%%The answer to everything is...
my_subfunction();
end
%%Functions
function [val]=my_mult(a, b)
val = a * b;
end
%%Tests
function workaround()
my_mult(0, 0);
end
function my_subfunction()
a = my_mult(7,6);
disp(a);
end
Maybe this won'r work for all cases, but, I hope, it will help someone.
  2 Comments
Jacob Mitchell
Jacob Mitchell on 3 Feb 2023
I'm mad that this still isn't fixed. I'm trying to do homework and be good by doing things in functions and Matlab makes me jump through hoops to publish it correctly
Jingze Li
Jingze Li on 10 Feb 2023
Same here, all my fprintf and codes are flying around. print result ended up in wrong sections

Sign in to comment.


per isakson
per isakson on 26 Dec 2012
Edited: per isakson on 26 Dec 2012
I have this comment in an old file of mine (FEX, tracer4m, Matlab 7.10)
Output in the wrong order. The tracer output above is created by disp(log) in
the last line of this cell! PUBLISH inserts the output in the wrong place.
That's because of the bug "#496201, Summary: Publishing a MATLAB file containing
subfunctions: sometimes the output appears in the wrong place in the document."
- I guess.
In Matlab Bug Reports List I just found
13 Dec 2005, 496201, Publishing a MATLAB file containing subfunctions: sometimes
the output appears in the wrong place in the document.
It's on "Watch", which I guess means that it is still in the code.
Report to the tech support. I'm sure The Mathworks want to add your example to their testsuite.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!