How to extract the first part of an array of strings by pattern?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
JFz
am 25 Mai 2017
Kommentiert: Image Analyst
am 25 Mai 2017
Hi,
I have an array of strings: L_Name. Each string has several "_" in it. I would like to have an array that take the first part in each string by '_'. I tried to use strfind, or reg, but just cannot get it.
How to do that? Thanks! JF
0 Kommentare
Akzeptierte Antwort
Stephen23
am 25 Mai 2017
Bearbeitet: Stephen23
am 25 Mai 2017
>> C = {'L_Name','Z_Y_X','Aaaa_BBBB','MMM'};
>> D = regexp(C,'[^_]+','once','match');
>> D{:}
ans = L
ans = Z
ans = Aaaa
ans = MMM
>>
2 Kommentare
Image Analyst
am 25 Mai 2017
The nice thing is, it also works for an array of strings (the new string data type that MATLAB now has) in addition to the cell array Stephen showed:
C = ["L_Name","Z_Y_X","Aaaa_BBBB","MMM"]
D = regexp(C,'[^_]+','once','match');
D{:}
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!