How to add string to the beginning of each element in a string array?

조회 수: 137 (최근 30일)
Ana Alonso
Ana Alonso 2018년 12월 12일
편집: TADA 2018년 12월 12일
Hi there,
I'm trying to append the string 'Syl_' to the beginning of each element in a string array. For example, if I have the string array
s1 = string(1:4)
Columns 1 through 4
"1" "2" "3" "4"
I want to attach 'Syl_' to the beginning of each element, yielding:
Columns 1 through 4
"Syll_1" "Syll_2" "Syll_3" "Syll_4"
Any help

채택된 답변

TADA
TADA 2018년 12월 12일
strcat('Syl_', s1)
ans =
1×4 string array
"Syl_1" "Syl_2" "Syl_3" "Syl_4"
  댓글 수: 2
TADA
TADA 2018년 12월 12일
편집: TADA 2018년 12월 12일
also works with a cell array of strings:
c1 = cellfun(@num2str, num2cell(1:4), 'UniformOutput', false);
strcat('Syl_', c1)
ans =
1×4 cell array
{'Syl_1'} {'Syl_2'} {'Syl_3'} {'Syl_4'}
Ana Alonso
Ana Alonso 2018년 12월 12일
Perfect! I didn't realize strcat could read in an array, thanks so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by