Equivalent of inputname() for output variables
Mostrar comentarios más antiguos
Say I have a function:
function B = myfun(A)
disp(inputname(1))
disp(outputname(1))
B = A;
If I call it from the command line as follows:
C = 1;
D = myfun(C)
I get:
C
Undefined function 'outputname' for input arguments of type 'double'.
Error in myfun (line 3)
disp(outputname(1))
That's because, unlike inputname(), which returns the name of the input variables in the caller workspace, an equivalent function for output variable names, outputname(), does not exist. My question is, is there any way of getting the names of output variables in the caller workspace?
6 comentarios
Georges
el 14 de Abr. de 2015
You're not alone. I was just searching for the solution to the same problem.
Geoff Hayes
el 15 de Abr. de 2015
Georges - why do you want the names of the output variables?
One use case: I would like to write a function that allows the user to develop the outputs interactively at the K>> prompt:
[A,B,C]=interactive('A','B','C')
function varargout=interactive(varargin)
strList="{"+strjoin(varargin,', ')+'}';
disp("Create the following variables at the K>> prompt : "+strList)
disp("Then issue DBCONT when done.");
keyboard
varargout=eval(strList);
end
However, the above implementation is a little clunky because I have to pass in the list of expected output variable names {'A','B','C'} as input to interactive(). With an outputname() function, this would be unnecessary. I could generate the list automatically.
Such an approach would fail if your function was called with something other than a raw variable name as an output argument. In that same situation for input arguments inputname returns ''. In each of those interactive() calls below, what would you hope to receive from the hypothetical outputname function for each of the output arguments?
c = cell(1, 3);
[c{:}] = interactive()
[a(1), b(2), d(4)] = interactive()
[~, n] = interactive()
e = cell(1, 2);
f = cell(1, 0);
g = cell(1, 1);
[e{:}, f{:}, g{:}] = interactive()
% Dummy interactive() function so the code above runs.
function varargout = interactive()
varargout = {1, 4, 27};
end
Matt J
el 30 de Nov. de 2022
what would you hope to receive from the hypothetical outputname function for each of the output arguments?
Similar to inputname, I would want outputname to return empty for such arguments. I would then parse the list and process only outputs with valid outputnames.
This is basically the same topic:
Respuesta aceptada
Más respuestas (1)
Matt J
el 1 de Dic. de 2022
0 votos
I've implemented Jan's idea in this FEX submission,
but it has some caveats (see below)
>> [A, ~, C]=func()
A =
'a'
C =
'c'
function varargout=func()
varargout=lower(outputnames);
end
The caveats are,
1. The line of code or the command line where the function call is
made must contain no other commands. The above example would fail,
had we done,
[A,B]=func(); [C,D]=func()
2. Function calls where the outputs contain indexing expressions
will have unjpredicatable behavior, e.g.,
[A{1,2}]=func()
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!