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!
Valid NamesA valid variable name starts with a letter, followed by letters, digits, or underscores. MATLAB® is case sensitive, so A and a are not the same variable. The maximum length of a variable name is the value that the namelengthmax command returns.
varname = genvarname(str) constructs a string or character vector varname that is similar to or the same as the str input, and can be used as a valid variable name. str can be a string, a string array, a character array, a cell array of character vectors.
varname=@(x) inputname(1);
disp(['Error in var: ' varname(mat)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With