How to format a series of filenames with different characters as endings?

1 view (last 30 days)
I am currently trying to find a way to create an array of filenames which all start with 'Channel01' but have different characters as endings. In the end I would want names which look like this: 'Channel01a', Channel01b', 'Channel01c', ... , 'Channel01t' and they should be stored under one variable.
I tried to the use of cellfun and sprintf:
unitletters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o','p', 'q', 'r', 's', 't']
units_filenames = cellfun(@x sprintf('Channel01%c',x),unitletters,'UniformOutput',false)
However, I always get an error message. Does anyone have an idea how to solve this issue?
  1 Comment
Stephen23
Stephen23 on 31 Aug 2022
Note that square brackets are a concatentation operator, so this line:
unitletters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o','p', 'q', 'r', 's', 't']
is trivially equivalent to
unitletters = 'abcdefghijklmnopqrst'
which is simpler and more robust using the colon operator:
unitletters = 'a':'t'

Sign in to comment.

Accepted Answer

Voss
Voss on 19 Aug 2022
Edited: Voss on 19 Aug 2022
Make unitletters a cell array, using {}, and fix your cellfun syntax:
unitletters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o','p', 'q', 'r', 's', 't'}
unitletters = 1×20 cell array
{'a'} {'b'} {'c'} {'d'} {'e'} {'f'} {'g'} {'h'} {'i'} {'j'} {'k'} {'l'} {'m'} {'n'} {'o'} {'p'} {'q'} {'r'} {'s'} {'t'}
units_filenames = cellfun(@(x)sprintf('Channel01%c',x),unitletters,'UniformOutput',false)
units_filenames = 1×20 cell array
{'Channel01a'} {'Channel01b'} {'Channel01c'} {'Channel01d'} {'Channel01e'} {'Channel01f'} {'Channel01g'} {'Channel01h'} {'Channel01i'} {'Channel01j'} {'Channel01k'} {'Channel01l'} {'Channel01m'} {'Channel01n'} {'Channel01o'} {'Channel01p'} {'Channel01q'} {'Channel01r'} {'Channel01s'} {'Channel01t'}
Or use the unitletters you had, which is a character array, and instead of cellfun/sprintf, use sprintfc:
unitletters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o','p', 'q', 'r', 's', 't']
unitletters = 'abcdefghijklmnopqrst'
units_filenames = sprintfc('Channel01%c',unitletters)
units_filenames = 1×20 cell array
{'Channel01a'} {'Channel01b'} {'Channel01c'} {'Channel01d'} {'Channel01e'} {'Channel01f'} {'Channel01g'} {'Channel01h'} {'Channel01i'} {'Channel01j'} {'Channel01k'} {'Channel01l'} {'Channel01m'} {'Channel01n'} {'Channel01o'} {'Channel01p'} {'Channel01q'} {'Channel01r'} {'Channel01s'} {'Channel01t'}

More Answers (1)

Walter Roberson
Walter Roberson on 19 Aug 2022
unitletters = 'a':'t';
units_filenames = "Channel01" + unitletters.'
units_filenames = 20×1 string array
"Channel01a" "Channel01b" "Channel01c" "Channel01d" "Channel01e" "Channel01f" "Channel01g" "Channel01h" "Channel01i" "Channel01j" "Channel01k" "Channel01l" "Channel01m" "Channel01n" "Channel01o" "Channel01p" "Channel01q" "Channel01r" "Channel01s" "Channel01t"
  1 Comment
Simon
Simon on 31 Aug 2022
Thanks for your answer, too. Also a nice way to do it. The other solution just came in a bit more handy for my problem.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!