How to take first character of Alphabet
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi,
I want to take the first character of the first alphabet from string contain mixed data,
I have below cell array:
data:
{'VA00K100E4TOO';'ZVA00K100E4TOO';'VZA00K100E6TO';'VB00K100E4TOO';'VP00K50E4T4O';'ZVG00K100E4TOO';'VF00K40E4T5O'}
1. I want to first letter of alphabet, ignore if first or second characters are Z or V.
I use below command to extract alphabets from each string,
data(isstrprop(data,'alpha'))
but later I am unable to avoid if the first or second character is Z or V,
my desired output:
A
A
A
B
P
G
F
댓글 수: 0
답변 (1개)
M
2018년 4월 5일
편집: M
2018년 4월 5일
One way to do it :
regexpi(data,'[a-u]','match','once')
ans =
7×1 cell array
{'A'}
{'A'}
{'A'}
{'B'}
{'P'}
{'G'}
{'F'}
댓글 수: 2
M
2018년 4월 5일
편집: M
2018년 4월 5일
To write a more generic version of my previous answer, you can use something like:
Alphabet = '[ABCDEFGHIJKLMNOPQRSTUVWXYZ]';
% choose which letters you want to suppress
lettersToRemove=['Z' 'V'];
% remove them from your alphabet list:
for i=1:numel(lettersToRemove)
Alphabet(Alphabet==lettersToRemove(i))=[];
end
% keep only the first letter of your alphabet list:
regexpi(data,Alphabet,'match','once')
ans =
7×1 cell array
{'A'}
{'A'}
{'A'}
{'B'}
{'P'}
{'G'}
{'F'}
% Now, suppose you want to remove V and G:
lettersToRemove=['V' 'G'];
for i=1:numel(lettersToRemove)
Alphabet(Alphabet==lettersToRemove(i))=[];
end
regexpi(data,Alphabet,'match','once')
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!