I have a function in Matlab that has some variables in it. I need to print out the variable_names (in case of an exception etc.). I am aware of inputname
function but it works for input_arguments only.
mat = [ 1 2 ; 3 4 ] ;
% disp(['Error in var: ' ??(a)])
% desired ouput: Error in var: mat (and NOT 1 2 ; 3 4!)
Thanks!
If you want to print out the variables present in a function, you can use the function WHO. Here's an example using a simple function
test.m
:Running this will give you the following output:
Matlab essentially does not let you do that. However, you can write a helper function to ease your pain in creating output like that:
which you could call like so in your case:
You can read more discussion on the topic on the Mathworks forum
Additionally, to list all current variables with values you can use the
who
function, but that is not the problem you presented.I'm adding another solution to the mix (one-liner):
Which outputs: