Help with displaying the vowels
Show older comments
Hi, I want to make my code to be converted to switch and case statements and have the desired output from the picture:

function [vowels] = locateVowels( charactercell )
isvowel=@(s) ismember(lower(s),'aeiou');
isvowel=@(s) ismember(lower(s),'aeiou');
idxV=cellfun(isvowel,charactercell,'unif',0);
vowels=cellfun(@(s,i) lower(s(i)),charactercell,idxV,'unif',0);
end
1 Comment
Rena Berman
on 30 Oct 2017
(Answers Dev) Restored edit
Accepted Answer
More Answers (1)
Use the string a cell array processing features of Matlab..
test={'Opal','Otis';'Fib','Lupe'};
isvowel=@(s) ismember(lower(s),'aeiou');
idxV=cellfun(isvowel,test,'unif',0);
cellfun(@(s,i) lower(s(i)),test,idxV,'unif',0)
ans =
'oa' 'oi'
'i' 'ue'
Presuming this is homework, I recommend you fully understand how it works and can reproduce it without coaching/help if asked to 'splain how you got the solution... :)
Hopefully will provide some ideas on how to more efficiently find the locations and you can then build your own solution based on those...or, if is real problem and not homework, you're more than welcome!! :)
12 Comments
Caleb Steel
on 10 Oct 2017
OK, does the above not then solve the problem in its entirety (other than as Walter show, the list of potential vowel characters is language dependent so have to fix that up to match your problem space)?
The difference in the two is that Walter's gives the vowels in each cell in the order in which they're found in the list of vowels; mine returns them in the order they're found in the input.
Just wrap the above in a function wrapper to pass in the input array...
function res=vowels(cellstrarray)
isvowel=@(s) ismember(lower(s),'aeiou');
idxV=cellfun(isvowel,cellstrarray,'unif',0);
res=cellfun(@(s,i) lower(s(i)),cellstrarray,idxV,'unif',0);
If the list of vowels isn't fixed, then could easily add it as an (optional) second parameter.
Caleb Steel
on 10 Oct 2017
Edited: Caleb Steel
on 10 Oct 2017
Walter Roberson
on 10 Oct 2017
You cannot just disp() those things. disp() displays them in the command window. You need to store them, like
counter = 0;
switch cell{1:end,2}
case 'a'
counter = counter + 1;
output(counter) = 'a';
case 'e'
counter = counter + 1;
output(counter) = 'e';
end
Then once you have stored them all in the order you want, you can display the output.
Note: do not use "cell" as the name of a variable: you will intefere with the important function cell()
Note: you had the wrong syntax for switch.
Note: really, though, the line
cellfun(@(s,i) lower(s(i)),test,idxV,'unif',0)
is going to display the results anyhow, because you did not assign the result to a variable and you did not put a semi-colon on the end of the line to suppress the output.
Note: really, though, all you need to do is assign the output of that cellfun to a variable and disp() the variable. The default output when you just allow a value to be displayed is different than if you disp() the variable. dpb happens to be running an older version which naturally output in the way you would prefer.
Caleb Steel
on 10 Oct 2017
Edited: Caleb Steel
on 10 Oct 2017
dpb
on 10 Oct 2017
"...I would like to see the code involving switch and case."
Why!?? switch construct has its place, but this really isn't it...just make more work than needs be.
Walter Roberson
on 10 Oct 2017
You do not return charactercell from the function, and you do not display it, so the editor is warning you that you are wasting time in computing it.
Are you sure you want to be writing over the top of your input ?
Are you sure that you want to be using a shared variable named "cell", thereby confusing the heck out of the people who are trying to read your code? Most of whom would assume that there "cell" is trying to refer to the function cell(), or else that "cell" is referring there to an uninitialized variable?
If the point is only to count the vowels then why bother storing them?
Caleb Steel
on 10 Oct 2017
Edited: Caleb Steel
on 10 Oct 2017
Walter Roberson
on 10 Oct 2017
Edited: Walter Roberson
on 10 Oct 2017
Sigh.
function [vowels] = locateVowels( charactercell )
vowels = '';
vowelcount = 0;
for row = 1 : length(charactercell)
thisentry = charactercell{row, 2};
switch thisentry
case 'a'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'a';
case 'ä'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ä';
case 'e'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'e';
case 'ë'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ë';
case 'é'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'é';
case 'i'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'i';
case 'ï'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ï';
case 'o'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'o';
case 'ö'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ö';
case 'u'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'u';
case 'ü'
vowelcount = vowelcount + 1;
vowels(vowelcount) = 'ü';
end
end
end
Note: all of the above are used in English. https://en.wikipedia.org/wiki/English_terms_with_diacritical_marks
Caleb Steel
on 11 Oct 2017
Edited: Caleb Steel
on 11 Oct 2017
Walter Roberson
on 11 Oct 2017
function [vowels] = locateVowels( charactercell )
vowels = cell(size(charactercell));
for cell_idx = 1 : numel(charactercell)
thesevowels = '';
vowelcount = 0;
thisentry = charactercell{cell_idx};
for str_idx = 1 : length(thisentry)
switch lower(thisentry(str_idx))
case 'a'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'a';
case 'ä'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ä';
case 'e'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'e';
case 'ë'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ë';
case 'é'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'é';
case 'i'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'i';
case 'ï'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ï';
case 'o'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'o';
case 'ö'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ö';
case 'u'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'u';
case 'ü'
vowelcount = vowelcount + 1;
thesevowels(vowelcount) = 'ü';
end
end
vowels{cell_idx} = thesevowels;
end
end
This is an approach I would never take, and is suitable only for an exercise in proving that one knows how to use the switch construct and cell arrays.
Caleb Steel
on 11 Oct 2017
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!