Equivalent of inputname() for output variables

47 views (last 30 days)
Oliver Woodford
Oliver Woodford on 21 Jul 2014
Edited: John D'Errico on 1 May 2019
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?
  2 Comments
Geoff Hayes
Geoff Hayes on 15 Apr 2015
Georges - why do you want the names of the output variables?

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);
  3 Comments
John D'Errico
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?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!