Equivalent of inputname() for output variables

19 views (last 30 days)
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 Comments
Matt J
Matt J on 30 Nov 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.

Sign in to comment.

Accepted Answer

Jan
Jan on 16 Apr 2015
Edited: Jan on 16 Apr 2015
You can do this by obtaining the caller tree by dbstack and parse the corresponding line of the calling M-file.
But I strongly recommend not to do this. The processing should not be based on the names of variables, because varaibales belong to the program, while the program should operate on the data. So mixing the program's source code with the operations on the data is a anti-pattern for clean programming. Such meta-programming increases the complexity of code dramatically. It is the opposite of the information-hiding principle in the object oriented programming. So even inputname is a strange command from this point of view.
Remember that such methods must fail, when the code is compiled.
When the names of the variables really matter, it would be a clean way to program this explicitly:
Data.A = 1:5;
Data.ReturnVariable = 'B';
Data = myfun(Data);
function Data = myfun(Data);
Data.(Data.ReturnVariable) = Data.A;
This is a little bit longer, but it is clean and clear. You cannot get obstacles like anonymous variables, as calls like this would cause with outputname:
B{3} = myfun(A);
or:
field = 'asd';
S.(field) = myfun(A);
  4 Comments
John D'Errico
John D'Errico on 1 May 2019
Edited: John D'Errico on 1 May 2019
Pooya89: If all you want is a function that can create a symbolic matrix with a given name, you could far more easily just pass in the desired name as an input argument. In fact, sym already does something at least close to that.
sym('J',[2 2])
ans =
[ J1_1, J1_2]
[ J2_1, J2_2]
Note that your numvering scheme would fail for symbolic arrays of size 11x11 or larger, as which element does J_111 indicate?
If you really needed to put this into the array of name J, syms already does that automatically.
clear
syms('J',[2 2])
whos
Name Size Bytes Class Attributes
J 2x2 8 sym
J1_1 1x1 8 sym
J1_2 1x1 8 sym
J2_1 1x1 8 sym
J2_2 1x1 8 sym
And it uses a better numbering scheme for the elements. So, while you could have written sym_mat to do exactly the same, why bother to recreate that wheel?
Matt J
Matt J on 30 Nov 2022
Edited: Matt J on 1 Dec 2022
You can do this by obtaining the caller tree by dbstack and parse the corresponding line of the calling M-file.
Unfortunately, that won't work if there is no calling mfile, i.e., if the function is invoked from the command line (though potentially you could parse History.xml).
It also won't work when the call is made using a function handle.
fhandle=@Func;
[A,B]=fhandle(); C=sin(pi) %(*)
function varargout=Func
varargout={1,2};
s=dbstack('-completenames');
currentFunction=s(1).name
end
In the example above, the command s=dbstack('-completenames') produces information that Func() is executing, but no information about which of the commands in line (*) has invoked it,
currentFunction =
'Func'

Sign in to comment.

More Answers (1)

Matt J
Matt J on 1 Dec 2022
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()

Community Treasure Hunt

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

Start Hunting!