How to suppress output of function when calling from another function?

7 次查看(过去 30 天)
So I have EulerMethod which when called itself I want it print the table with x = and y =. But when I call EulerMethod from LeapfrogMethod I don't want the one line of x = and y= to be outputted - is there a way to do this? (not semi-colon)
EulerMethod.m
function [ matrix ] = EulerMethod( fun, initX, initY,...
steplength, maximum )
...
fprintf('x = %3g, y = %3g\n',matrix(J,1),matrix(J,2))
...
end
LeapfrogMethod.m
function [ matrix ] = LeapfrogMethod( fun, initX, initY,...
steplength, maximum )
...
euler = EulerMethod(f,x0,y0,h,h);
...
end

采纳的回答

Stephen23
Stephen23 2015-8-19
编辑:Stephen23 2015-8-19
Probably the easiest way of doing (almost) what you want is to use nargout. MATLAB uses this with several of their basic functions: when the function is called without any output arguments, then it simply prints to the command window, otherwise it returns only the output arguments. You can choose the logic yourself:
if nargout==0
fprintf(...)
end
For an example of this see calendar.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by