How to return no output in a function with output?

조회 수: 281 (최근 30일)
Laura Alvarez
Laura Alvarez 2020년 1월 4일
댓글: Stephen23 2020년 1월 4일
I have a function which should return two outputs, but if a condition is satisfied (in my case if a matrix is singular) it is no posible to calculate any output but the function returns the outputs. I would like to know if there is any posibility in order to return no output. This is my function:
function [x, NumIter] = Newton(fun, Jfun, x0, tol, IterMax)
x = x0;
NumIter = 0;
funx = fun(x);
Jfunx = Jfun(x);
if rank(Jfunx)<length(Jfunx)
disp('The matrix is singular')
return
end
...
end
In this case, if the matrix is singular, the function returns the initial x and the initial number of iterations. Would be possible not to return them?
THANK YOU SO MUCH !!
  댓글 수: 1
Stephen23
Stephen23 2020년 1월 4일
"Would be possible not to return them?"
It would certainly be possible, but then you would also have to call the function without any output arguments in order to avoid an error. I doubt that this would be a useful approach. The standard MATLAB approach is to return a value that is clearly not a data value, e.g. an empty array or NaN.

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

채택된 답변

Image Analyst
Image Analyst 2020년 1월 4일
You MUST return something or else you'll get an error. And it's good that you defined them to be something in the very first lines of the function (most people mistakenly do not).
However, if you want, you can return null.
x = [];
NumIter = [];
disp('The matrix is singular')
return
just be sure to examine what's returned, null or actual data, when you call the function from your main program.

추가 답변 (1개)

TADA
TADA 2020년 1월 4일
output variables, the same as input variables, are optional
You can choose to return one, both or neither of your outputs.
In matlab returning an output is done by setting the variable, if you never set it, it will not be returned
% this example shows how you can return or not return outputs
% with respect to some calculation
% the actual returned values are meaningless,
% but it answers the general idea behind your question
function [a, b] = foo(x)
if x == 0
% nothing is returned
return;
else
M = rand(x);
a = sum(M, 'all');
if a > 10
b = M(end, end);
end
end
end
You should also remember that because Matlab functions are not too strict about number of parameters, overloading often depends on the number of sent input arguments or the number of requested output arguments. Therefore, someone may expect you to return both while the function returns only one parameter:
% rand(2) will never sum up to 10, so b will not be returned
% therefore, this line of code will always raise an error
foo(2); % this is fine
a = foo(2); % this is also fine
[a,b] = foo(2); % this will throw an exception
a = foo(0); % this will also raise an error
This is quite an unexpected outcome.
To resolve this, you can use nargout to determine how many output arguments the user expects
function [a, b] = foo(x)
% This will prevent the error from before, by setting the output to empty values
% this should obviously be well documented so that the output of the function is
% predictable
if nargout >= 1; a = []; end
if nargout >= 2; b = []; end
if x == 0
% nothing is returned
return;
else
M = rand(x);
a = sum(M, 'all');
if a > 10
b = M(end, end);
end
end
end

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by