0

o I am a neuroscience/biomedical engineering major currently taking a class in programming MATLAB. I have a hw program I have run into a problem with. I am super close to figuring it out. Basically I have to take a phrase like "woot I love matlab' and get out 'wtILvMtlb'. I've removed the spaces, and I've removed the vowels, but I can't seem to get it to capitalize the letters after the spaces. When I try, the second I take out the spaces, it goes a little crazy. I technically haven't learned the regexprep function either, but since I found it I figured I might as well use it. The teacher doesn't care. She probably wants us to do some indexing stuff to get our result, but if the regexprep works, I'll take that.

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;

I need to convert 'one fish two fish three fish' into 'onFshTwFsh'. I'm close, but not quite there. This is my final issue. My code takes out the first o. Indexing doesn't seem to be working. I've tried to index it like six different ways.

Jessica Marie
  • 293
  • 5
  • 16
  • So is `firstWord = "I love matlab' and get out 'ILvMtlb'`? This doesn't work with MATLAB. Also, what's the expected output? – Divakar Sep 19 '14 at 04:08
  • @Divakar Technically one of my test cases is "woot I love matlab' and I have to camelcase it. In other words, I need to make it say 'wtILvMtlb'. I need to capitalize the letters after the space, but when I pull out the spaces, it stops working. – Jessica Marie Sep 19 '14 at 11:29
  • @Divakar It's possible in a long series of steps. 'ILvMtlb' is the eventual output – Jessica Marie Sep 19 '14 at 12:56

2 Answers2

2

Using regexprep of course works, but its a bit of overkill, and anyway this way you won't learn how to program Matlab. This is doable in "pure Matlab" in a very clean and clear way:

ind = find(firstWord(1 : end - 1) == ' ');
firstWord(ind + 1) = upper(firstWord(ind + 1));
firstWord(ismember(firstWord, ' aeiou')) = []
A. Donda
  • 8,381
  • 2
  • 20
  • 49
  • @A.Donda Now I tried something like that, but I was trying to do it using the findstr function. Originally I had is so I found each of the vowels in the string individually, then pulled them out. I did the same for spaces, but it was capitalizing after a space, then pulling it out that gave me issues. – Jessica Marie Sep 19 '14 at 13:15
  • You might want to consider defending against a ` ` being the final character of the string. Perhaps something like `replace = ind + 1; replace(replace > length(firstWord)) = [];` – Edric Sep 19 '14 at 15:20
  • I don't think I have to worry about that. my main focus now is keeping the first letter in the string, if it is a vowel – Jessica Marie Sep 19 '14 at 16:32
  • @JessicaMarie You can use Edric's solution and add this - `match1 = ismember(result,'AEIOU'), lower_result = lower(result), result(match1) = lower_result(match1)` – Divakar Sep 19 '14 at 16:39
  • That seems like it makes a lot of sense. I'm just struggling a little with figuring out where to plug it in. I'm attempting to move things around, but it either puts back in vowels, or still deletes the first vowel – Jessica Marie Sep 19 '14 at 16:49
  • @Edric – I have. Have a closer look at the first line. :-) – A. Donda Sep 19 '14 at 17:01
  • @JessicaMarie: Not clear about what should happen if the first letter of a word is a vowel. Should it be kept, capitalized? If not, should the following letter be capitalized. The former is what my code does, in order to get the second, just put the third line at the beginning. – A. Donda Sep 19 '14 at 17:04
  • The first letter of the string should always be lowercased, but if it's a vowel, I don't want it to be taken out. So I need to put the lowercased vowel back in (but only in the first letter) – Jessica Marie Sep 19 '14 at 18:29
  • @JessicaMarie After you have `result` from Edric's solution, add this - `match1 = ismember(result,'AEIOU'), lower_result = lower(result), result(match1) = lower_result(match1)` – Divakar Sep 20 '14 at 14:47
1

You can do this with a couple of calls to REGEXPREP.

>> str = 'i love matlab'
str =
i love matlab
>> cap = regexprep(str, '(?<=(^| ))(.)', '${upper($1)}')
cap =
I Love Matlab
>> result = regexprep(cap, '[aeiou ]', '')
result =
ILvMtlb

The first REGEXPREP uses a "lookahead" operator to find spaces or the beginning of the string, and then picks the next character and replaces it with the upper-case version.

The second REGEXPREP simply uses a character group to replace vowels and spaces with nothing. Depending on whether you want to remove capitalised vowels too, you might need to use [aeiouAEIOU ] as the character group.

Edric
  • 23,676
  • 2
  • 38
  • 40
  • Thank you, but when I try to us the last one, it flags me. It says undefined input for argument char. Not too sure why. – Jessica Marie Sep 19 '14 at 11:31
  • Sorry - I had a mistake in there (now fixed) - I thought there existed a function called `regexprepi` but there does not. – Edric Sep 19 '14 at 12:47
  • That works (mostly) perfectly thanks :) I just need to lowercase the very first letter. I should be able to do that with indexing, I hope. One of my test cases is 'woot I love matlab' so the w needs to be lowercase. The regexprep makes is uppercase for some reason. I appreciate all the help though :) – Jessica Marie Sep 19 '14 at 13:09
  • If you don't want to capitalize the first letter, you can use a slightly different regular expression in the first call - `'(?<= )'` should do the trick. – Edric Sep 19 '14 at 15:18
  • I'll give it a try. Do you have any suggestions on not killing the first letter if it's a vowel? I tried to do indexing, but that didn't pan out – Jessica Marie Sep 19 '14 at 15:20