4

I found a simple way to check whether a letter is a vowel in MATLAB (from here), simply like this :

is_vowel = numel(regexp(letter,'[aeiouAEIOU]'))>0

However, in my language (Turkish), the list of vowels is actually this:

is_vowel = numel(regexp(letter,'[aeiouöüıAEIOUÖÜİ]'))>0

this works correctly for ö and ü (Also for capitals) but it returns 0 for ı and İ. How can I solve this issue so it returns 1 for all the letters in the second list?

P.S. : I will use this code as a base for the latter part of my project which will involve things like getting the number of vowels in a word, so I'm open to alternative solutions too.

Also, if no other way is found, I'm ok with converting the letters to their English counterparts (like Ö to O etc.) but I prefer not changing them.

Thanks for any help!

Edit

I now see that the related part of my .m file is converted to this :

regexp(letter,'[aeiouöü?AEIOUÖÜ?]');

This is probably why regex is not working on those two characters. Any way to save these characters correctly in an *.m file?

Solution

Thanks to @AndrasDeak :

function [b] = is_vowel(letter)
b = ismember(letter,['aeiouöüAEIOUÖÜ' 304 305]);
end
jeff
  • 13,055
  • 29
  • 78
  • 136

1 Answers1

4

As we revealed in comments, the problem is that some Turkish characters are incompatible with matlab, but only as string literals. The solution is to check the problematic letters by their value rather than as characters.

A fast solution is based on using ismember to check if the given letter is among the set of Turkish vowels:

b = ismember(letter,['aeiouöüAEIOUÖÜ' 304 305]);

This makes use of the fact that strings are integer vectors under the hood.

And just to overdo it a bit, you can also define this as an anonymous function:

is_vowel = @(letter) ismember(letter,['aeiouöüAEIOUÖÜ' 304 305]);