필터 지우기
필터 지우기

How to take first character of Alphabet

조회 수: 5 (최근 30일)
Mekala balaji
Mekala balaji 2018년 4월 5일
편집: M 2018년 4월 5일
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

답변 (1개)

M
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
Mekala balaji
Mekala balaji 2018년 4월 5일
Sir,
it works,but may I know how does it work? if I want avoide other alphabets (like if first or second letter is N or T etc), and the very first alphabet,
M
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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by