varfun: how to get ride of the function name for the output variable?

7 views (last 30 days)
When I apply varfun, the name of the function will show up in the returned variable name. For example, when I use
Strings = varfun(@string, x, 'inputvariables', 'Var1')
The returned variable name will be
string_Var1
My question is, is there a way to keep the original variable name instead of having ('string_') there?

Accepted Answer

dpb
dpb on 8 Dec 2019
No. That's the way varfun is designed and works to keep from overwriting the original variable with the functional output having operated on the input variable(s). A table cannot have two columns with the same variable name and undoubtedly in most cases not overwriting the original variable would be the desired action--and certainly not being able to identify a new variable created from the original wouldn't be agoodthing™ when the new variable is something far different than the original.
I suppose it wouldn't harm anything to submit an enhancement request to have a way to cause that action, but I have my doubts TMW would see it as enough of a need as to have much likelihood at all of implementation.
That said, if you don't want the original variable any longer, you can delete it and then rename the functional column to that name; that can be done programmatically as the variables names are available from the xxx.Properties.VariableNames struct.
  3 Comments
dpb
dpb on 9 Dec 2019
Unless you rewrite the changed names back into the structure, nothing will change...
Strings.Properties.VariableNames=regexprep(Strings.Properties.VariableNames, 'string_', '');

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!