name of variables from a cell of strings

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 件のコメント

Star Strider
Star Strider 2014 年 5 月 15 日
Please don’t!
Make arrays out of them instead.

サインインしてコメントする。

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 15 日
編集済み: Azzi Abdelmalek 2014 年 5 月 15 日

0 投票

b={'alpha', 'beta', 'gamma'}
c=cellfun(@(x) ['x_' x],b,'un',0)
Look at

1 件のコメント

Jos (10584)
Jos (10584) 2014 年 5 月 15 日
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.

サインインしてコメントする。

Jos (10584)
Jos (10584) 2014 年 5 月 15 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

質問済み:

2014 年 5 月 15 日

回答済み:

2014 年 5 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by