MATLAB Answers

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

14 views (last 30 days)
ben velt
ben velt on 23 May 2015
Answered: Walter Roberson on 23 May 2015

" 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.

  0 Comments

Sign in to comment.

Accepted Answer

pietro
pietro on 23 May 2015
Edited: pietro on 23 May 2015
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

  0 Comments

Sign in to comment.

More Answers (1)

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

  0 Comments

Sign in to comment.

Sign in to answer this question.