Combining variables
이전 댓글 표시
I have written a code that generates 20 strings (the number actually varies depending on the input), all of which have the names "s_idx_?" where ? is a number from 1 to 20. Is there a command that allows me to concatenate all these strings without having to type out all the names? Thanks.
댓글 수: 5
Nathan Greco
2011년 7월 19일
First: it is generally nicer to create one data structure to contain all of these strings rather than creating a variable per string. Look up cell arrays, they are your friend. Rather than calling your strings s_idx_?, you could just call them s{?} where ? is your old number.
E. Elgar
2011년 7월 19일
Sean de Wolski
2011년 7월 19일
POISON!! EVAL!! Don't do it!!
E. Elgar
2011년 7월 20일
채택된 답변
추가 답변 (1개)
Jan
2011년 7월 20일
Follow Sean's link to the FAQ. There you find the suggestion to use a cell like "s_idx{i}" instead of including the index in the name of the variables. Thjen the concatenation is easy:
s = cat(2, s_idx{:});
카테고리
도움말 센터 및 File Exchange에서 Variables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!