name of variables from a cell of strings

8 vues (au cours des 30 derniers jours)
Kanghyun
Kanghyun le 15 Mai 2014
Let b={'alpha', 'beta', 'gamma'}
What is the best way to generate variables whose name contains each element of b? For example how do I generate variables x_alpha, x_beta, x_gamma? from b?
  1 commentaire
Star Strider
Star Strider le 15 Mai 2014
Please don’t!
Make arrays out of them instead.

Connectez-vous pour commenter.

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 15 Mai 2014
Modifié(e) : Azzi Abdelmalek le 15 Mai 2014
  1 commentaire
Jos (10584)
Jos (10584) le 15 Mai 2014
If you just want to add a 'x_' before every string of b, you're way better off using the dedicated function STRCAT.
b = {'alpha', 'beta', 'gamma'}
c = strcat('x_', b)
But still, dot not use these a variable names! Use structs instead.

Connectez-vous pour commenter.


Jos (10584)
Jos (10584) le 15 Mai 2014
Do not do this! Use structs
b={'alpha', 'beta', 'gamma'}
x.(b{1}) = 1:3
x.(b{2}) = 'hello'
x.(b{3}) = NaN

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by