How to return no output in a function with output?
170 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Laura Alvarez
el 4 de En. de 2020
Comentada: Stephen23
el 4 de En. de 2020
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 comentario
Stephen23
el 4 de En. de 2020
"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.
Respuesta aceptada
Image Analyst
el 4 de En. de 2020
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.
0 comentarios
Más respuestas (1)
TADA
el 4 de En. de 2020
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.
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
0 comentarios
Ver también
Categorías
Más información sobre Data Type Identification en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!