How to format a series of filenames with different characters as endings?
2 views (last 30 days)
Show older comments
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
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'
Accepted Answer
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'}
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)
More Answers (1)
See Also
Categories
Find more on Matrices and Arrays 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!