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

조회 수: 2 (최근 30일)
Simon
Simon 2022년 8월 19일
댓글: Stephen23 2022년 8월 31일
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
Stephen23
Stephen23 2022년 8월 31일
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'

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 8월 19일
편집: Voss 2022년 8월 19일
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'}

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 8월 19일
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
Simon
Simon 2022년 8월 31일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by