genvarname({'A', 'A', 'A', 'A'});
Show older comments
Hi I would like to use genvarname({'A'}) but it should be dynamic times, like A1,A2,A3,....,An, as much as i want. I am not interest to write it like v = genvarname({'A', 'A', 'A', 'A'}); (5 times). Hope you the people understand why I do not want to write of it.
3 Comments
It is such a terrible idea to generate variable names dynamically. No matter how much beginners keep inventing it and thinking that it would be a great idea for their code, this is one of the worst ways to write code.
Make your code faster, robuster, and neater by avoiding dynamically defined variable names. Properly written code is easier to understand, easier top debug, and easier to write: dynamically defined variable names are the opposite of all of these:
Azzi Abdelmalek
on 22 Apr 2016
Nothing is indicating that he is creating variables, they are just strings!
Sean de Wolski
on 22 Apr 2016
I'll give him the benefit of the doubt that he is setting the VariableNames property of a table. This will likely be more and more common as tables start to get more use and the default variable names of Var1, Var2, ... Varn are less explanatory than Sensor1,Sensor2, ... SensorN.
Accepted Answer
More Answers (2)
Jos (10584)
on 22 Apr 2016
0 votes
Why do you want to do this? It is generally a very bad programming idea to generate variable names like this.
"It is the contents of a variable that should change, not the name of the variable!"
Jos (10584)
on 22 Apr 2016
GENVARNAME will become obsolete pretty soon. This is more flexible:
C = arrayfun(@(k) sprintf('Sensor%02d',k), 7:13, 'un',0)
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!