From: John on
Forgive me if this post seems redundent to you, but I still have problem when trying to use self-defined function to flip any matrix and give it a new name by adding a suffix "_rows_reversed".

The function I wrote is:

function reverse(var_input)
getname=@(varargin) inputname(1);
var_name_prefix = getname(var_input);
var_name_suffix = '_rows_reversed';
var_name = strcat(var_name_prefix, var_name_suffix);
assignin('base', var_name, flipud(var_input));
end

For example, there's a matrix AAA=[1,2,3; 4,5,6; 7,8,9]

By using the function reverse(AAA) shoud produce a new matrix named AAA_rows_reversed=[7,8,9; 4,5,6; 1,2,3]

However, no matter what name the input variable is what I got is always a new matrix named var_input_rows_reversed=[7,8,9; 4,5,6; 1,2,3]

Why the get variable name script "getname(var_input)" doesn't work in this case? Is it because it's be used within a function because it surely works in command lines? Please advise me on this?

Again, thank all of you very much who have replied my post and provide me invaluable suggestions!!!
From: Steven Lord on

"John " <wwwinfor(a)gmail.com> wrote in message
news:hmf8cc$2n9$1(a)fred.mathworks.com...
> Forgive me if this post seems redundent to you, but I still have problem
> when trying to use self-defined function to flip any matrix and give it a
> new name by adding a suffix "_rows_reversed".
> The function I wrote is:
>
> function reverse(var_input)
> getname=@(varargin) inputname(1);

This returns the name of the first input of the anonymous function getname,
not the function reverse.

> var_name_prefix = getname(var_input);
> var_name_suffix = '_rows_reversed';
> var_name = strcat(var_name_prefix, var_name_suffix);
> assignin('base', var_name, flipud(var_input));

I strongly recommend you don't do this. For one thing, you're "poofing" a
variable into the base workspace, for another doing "tricky" things like
this make programs VERY difficult to debug if something goes wrong, and for
yet another you're assuming that the user doesn't call your function with a
literal or some temporary expression for which INPUTNAME returns an empty
string:

reverse(1:10)
reverse(AAA(1:2, 2:3))

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ