How can I keep the first two elements from CSV values in a string
조회 수: 3 (최근 30일)
이전 댓글 표시
I have a Table where one of the columns contains text elements separated by commas. I want to keep the first element (if there is only one) or the first two (if the are more than one). There also could be empty lines. For example, from:
abcd, efgh, tyui
[]
lkjh, poiu
wert
I want to get
abcd, efgh
[]
lkjh, pou
wert
I know how to do this with a for loop. But the idea would be to use regular expressions or something similar to accelerate the process. The Table has almost 2M elements.
Any suggestion would be of great help. Thanks.
댓글 수: 0
채택된 답변
추가 답변 (1개)
Walter Roberson
2024년 10월 1일
FilteredData = regexp(YourTable.ColumnName, '^[^,]+(,\s+[^,]+)?', 'match', 'once');
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!