What is the best way to get the name of a variable in a script?
The below code works but I am wondering if there is a built-in function that does this:
getname = @(x) inputname(1); var = 75; name = getname(var);
The reason I want the name of a variable is I don't want to hard code the name of a variable into a save command like this:
save('Myfile.mat', 'var');
This seems better:
save('Myfile.mat', getname(var));
When you rename a variable, the hard coded save command breaks.
Best Answer