Borrar filtros
Borrar filtros

Check which output arguments are requested within a function

22 visualizaciones (últimos 30 días)
Maverick
Maverick el 23 de Ag. de 2017
Respondida: Jan el 23 de Ag. de 2017
I have a custom function that returns multiple arguments, say:
function [arg1, arg2, arg3, arg4] = myfun(x,y,z)
% Code
end
Now, we can call that function e.g. in such way:
[A, B, ~, D] = myfun(3,2,1)
If we make such call, no value has to be assigned to variable arg3 within myfun. I want to know if there is a way to check within the function body which output arguments have been called (same as arg1, arg2, arg4 above), and which outputs have been neglected (i.e. arg3 which is called with tilde).
There is a number of functions dealing with i/o arguments, but I haven't been able to find a solution to this question in the documentation of 2015a version.
  1 comentario
Adam
Adam el 23 de Ag. de 2017
Have you nosied into the code of Matlab functions that do this to see what they do? I don't know off-hand. They may just calculate all results anyway or they may have a more intelligent method.
Certainly nargout is not of use here since you are requesting non-contiguous output arguments.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 23 de Ag. de 2017
Editada: Stephen23 el 23 de Ag. de 2017

Más respuestas (1)

Jan
Jan el 23 de Ag. de 2017
You can implement this manually:
function Out = myfun(x,y,z, Want)
% Want: cell string containing the names of the wanted outputs:
if any(strcmpi(Want, 'A'))
Out.A = rand(1);
end
if any(strcmpi(Want, 'B'))
Out.B = 'any string';
end
...
end
And automatic solution with identifying the "~" would be much nicer. Note that Matlab does have this information during the runtime already, but there is no way to provide this detail to the called function. I assume this is built into the design of Matlab, because this would require forwarding information to a function, which is used in the future -- after leaving the function.
You can send an enhancement request to MathWorks.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by