Publishing result order when using functions
27 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Roman Kogan
2015-11-5
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 个评论
Jacob Mitchell
2023-2-3
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
2023-2-10
Same here, all my fprintf and codes are flying around. print result ended up in wrong sections
per isakson
2012-12-26
编辑:per isakson
2012-12-26
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!