필터 지우기
필터 지우기

Publishing result order when using functions

조회 수: 8 (최근 30일)
Lowell Toms
Lowell Toms 2012년 12월 26일
댓글: Jingze Li 2023년 2월 10일
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?

답변 (2개)

Roman Kogan
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
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
Jingze Li 2023년 2월 10일
Same here, all my fprintf and codes are flying around. print result ended up in wrong sections

댓글을 달려면 로그인하십시오.


per isakson
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.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by