How to take first character of Alphabet

5 Ansichten (letzte 30 Tage)
Mekala balaji
Mekala balaji am 5 Apr. 2018
Bearbeitet: M am 5 Apr. 2018
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

Antworten (1)

M
M am 5 Apr. 2018
Bearbeitet: M am 5 Apr. 2018
One way to do it :
regexpi(data,'[a-u]','match','once')
ans =
7×1 cell array
{'A'}
{'A'}
{'A'}
{'B'}
{'P'}
{'G'}
{'F'}
  2 Kommentare
Mekala balaji
Mekala balaji am 5 Apr. 2018
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 am 5 Apr. 2018
Bearbeitet: M am 5 Apr. 2018
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')

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by