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 commentaires

Stephen23
Stephen23 le 22 Avr 2016
Modifié(e) : Stephen23 le 22 Avr 2016
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
Azzi Abdelmalek le 22 Avr 2016
Nothing is indicating that he is creating variables, they are just strings!
Sean de Wolski
Sean de Wolski le 22 Avr 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.

Connectez-vous pour commenter.

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 22 Avr 2016

0 votes

genvarname(repmat({'A'},1,5),'A')

1 commentaire

Rasel Biswas
Rasel Biswas le 22 Avr 2016
Thanks for the simplest answer but it works for me.

Connectez-vous pour commenter.

Plus de réponses (2)

Jos (10584)
Jos (10584) le 22 Avr 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)
Jos (10584) le 22 Avr 2016

0 votes

GENVARNAME will become obsolete pretty soon. This is more flexible:
C = arrayfun(@(k) sprintf('Sensor%02d',k), 7:13, 'un',0)

Community Treasure Hunt

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

Start Hunting!

Translated by