Filter löschen
Filter löschen

Check which output arguments are requested within a function

22 Ansichten (letzte 30 Tage)
Maverick
Maverick am 23 Aug. 2017
Beantwortet: Jan am 23 Aug. 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 Kommentar
Adam
Adam am 23 Aug. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 23 Aug. 2017
Bearbeitet: Stephen23 am 23 Aug. 2017

Weitere Antworten (1)

Jan
Jan am 23 Aug. 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.

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by