Azzera filtri
Azzera filtri

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

22 visualizzazioni (ultimi 30 giorni)

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

Risposta accettata

pietro
pietro il 23 Mag 2015
Modificato: pietro il 23 Mag 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

Più risposte (1)

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

Categorie

Scopri di più su Numeric Types in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by