Combining variables

4 views (last 30 days)
E. Elgar
E. Elgar on 19 Jul 2011
Edited: Stephen23 on 12 Sep 2023
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 Comments
E. Elgar
E. Elgar on 20 Jul 2011
!! Other solutions then?

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 19 Jul 2011
Don't do it!
but if you have to:
sprintf('s_idx_%i\n',1:20)
  1 Comment
Jan
Jan on 20 Jul 2011
I agree with "Don't do it". The SPRINTF command concatenates the _names_ of the variables?! The OP wants to cat the string values.

Sign in to comment.

More Answers (1)

Jan
Jan on 20 Jul 2011
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{:});
  1 Comment
E. Elgar
E. Elgar on 20 Jul 2011
Thank you! I think this will work.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!