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
2014년 5월 15일
Please don’t!
Make arrays out of them instead.
답변 (2개)
Azzi Abdelmalek
2014년 5월 15일
편집: Azzi Abdelmalek
2014년 5월 15일
b={'alpha', 'beta', 'gamma'}
c=cellfun(@(x) ['x_' x],b,'un',0)
Look at
댓글 수: 1
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)
2014년 5월 15일
Do not do this! Use structs
b={'alpha', 'beta', 'gamma'}
x.(b{1}) = 1:3
x.(b{2}) = 'hello'
x.(b{3}) = NaN
카테고리
도움말 센터 및 File Exchange에서 Gamma Distribution에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!