How can i split between strings that connected with under score?

조회 수: 23 (최근 30일)
ben velt
ben velt 2015년 5월 23일
답변: Walter Roberson 2015년 5월 23일

" x= 'Penelope_Cruz' 'Gary_Cooper' 'Tony_Curtis' 'Jim_Carrey'"

I want to split x into two different variables, like this:

a= 'Penelope' 'Gary' 'Tony' 'Jim'

b= 'Cruz' 'Cooper' 'Curtis' 'Carrey'

I want to do it for unknown amount of names.

채택된 답변

pietro
pietro 2015년 5월 23일
편집: pietro 2015년 5월 23일
one solution:
x={ 'Penelope_Cruz' 'Gary_Cooper' 'Tony_Curtis' 'Jim_Carrey'};
for i=1:length(x)
temp=strsplit(x{i},'_');
a(i)=temp(1);
b(i)=temp(2);
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 5월 23일
T = regexp(x, '_', 'split');
a = cellfun(@(C) C{1}, T);
b = cellfun(@(C) C{2}, T);

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by