Search code examples
matlabindexingcamelcasing

Keeping the first vowel, while removing the rest MATLAB


I hate asking this, but I am so close to figuring it out and it's really bothering me. I need to switch my strings into camelcase. I got rid of the spaces, I can uppercase the correct letters and I can remove the vowels, but I need to keep the very first letter of the code and I can't seem to be able to get it to stay. I've tried indexing it six different ways, to no avail.

function[cameltoe] = abbreviatingCamelCase(firstWord)
indexing = find(firstWord(1:end - 1) == ' ');%I want to find all the spaces here
firstWord( indexing + 1) = upper(firstWord(indexing + 1)); %I want to uppercase all the words     following a space
firstWord(firstWord == ' ') = [];
firstWord(ismember(firstWord, ' aeiou')) = [];
cameltoe = firstWord;

'one fish two fish red fish blue fish' should turn into 'onFshTwFshRdFshBlFsh'. I've been working on this for at least two hours. I tried indexing the firstWord inside where the ' aeiou' is, but that doesn't seem to work.


Solution

  • Hope you don't mind that I created an extra variable. Is this what you're looking for?

    firstWord = 'one fish two fish red fish blue fish'
    indexing = find(firstWord(1:end - 1) == ' ');%I want to find all the spaces here
    firstWord( indexing + 1) = upper(firstWord(indexing + 1)); %I want to uppercase all the words     following a space
    firstWord(firstWord == ' ') = [];
    Li = ismember(firstWord, 'aeiou');
    Li(find(Li,1,'first'))=0;
    firstWord(Li) = [];
    cameltoe = firstWord
    

    Edit: if you want to keep the first letter, regardless of being a vowel:

    indexing = find(firstWord(1:end - 1) == ' ');
    firstWord( indexing + 1) = upper(firstWord(indexing + 1)); 
    firstWord(firstWord == ' ') = [];
    firstWord([false ismember(firstWord(2:end), 'aeiou')]) = [];
    cameltoe = firstWord;