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

1 visualizzazione (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 31 Ago 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'

Accedi per commentare.

Risposta accettata

Voss
Voss il 19 Ago 2022
Modificato: Voss il 19 Ago 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'}

Più risposte (1)

Walter Roberson
Walter Roberson il 19 Ago 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 Commento
Simon
Simon il 31 Ago 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.

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by