How to format a series of filenames with different characters as endings?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
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'
Risposta accettata
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'}
units_filenames = cellfun(@(x)sprintf('Channel01%c',x),unitletters,'UniformOutput',false)
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']
units_filenames = sprintfc('Channel01%c',unitletters)
Più risposte (1)
Vedere anche
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!