MATLAB Answers

Subroutines stops at input

2 views (last 30 days)
Justiz Baker
Justiz Baker on 22 Mar 2020
Answered: Image Analyst on 22 Mar 2020
I am creating a program that will be able to receive string input and count the number of vowels and desired letter by the user. I am trying run my subroutine but it keeps stopping at the input: here is the main function code:
clear all;
clc;
[II] = input('Please enter your quote','s')
[n,m] = size(II);
sa = 0;
sb = 0;
sc = 0;
sd = 0;
se = 0;
sf = 0;
sg = 0;
sh = 0;
si = 0;
sj = 0;
sk = 0;
sl = 0;
sm = 0;
sn = 0;
so = 0;
sp = 0;
sq = 0;
sr = 0;
ss = 0;
st = 0;
su = 0;
sv = 0;
sw = 0;
sx = 0;
sy = 0;
sz = 0;
for ii = 1:m
if II(1,ii) == 'a' | II(1,ii) =='A'
sa = sa + 1;
elseif II(1,ii) == 'b'| II(1,ii) =='B'
sb = sb + 1;
elseif II(1,ii) == 'c'| II(1,ii) =='C'
sc = sc + 1;
elseif II(1,ii) == 'd'| II(1,ii) =='D'
sd = sd + 1;
elseif II(1,ii) == 'e'| II(1,ii) =='E'
se = se + 1;
elseif II(1,ii) == 'f'| II(1,ii) =='F'
sf = sf + 1;
elseif II(1,ii) == 'g'| II(1,ii) =='G'
sg = sg + 1;
elseif II(1,ii) == 'h'| II(1,ii) =='H'
sh = sh + 1;
elseif II(1,ii) == 'i'| II(1,ii) =='I'
si = si + 1;
elseif II(1,ii) == 'j'| II(1,ii) =='J'
sj = sj + 1;
elseif II(1,ii) == 'k'| II(1,ii) =='K'
sk = sk + 1;
elseif II(1,ii) == 'l'| II(1,ii) =='L'
sl = sl + 1;
elseif II(1,ii) == 'm'| II(1,ii) =='M'
sm = sm + 1;
elseif II(1,ii) == 'n'| II(1,ii) =='N'
sn = sn + 1;
elseif II(1,ii) == 'o'| II(1,ii) =='O'
so = so + 1;
elseif II(1,ii) == 'p'| II(1,ii) =='P'
sp = sp + 1;
elseif II(1,ii) == 'q'| II(1,ii) =='Q'
sq= sq + 1;
elseif II(1,ii) == 'r'| II(1,ii) =='R'
sr = sr + 1;
elseif II(1,ii) == 's'| II(1,ii) =='S'
ss= ss + 1;
elseif II(1,ii) == 't'| II(1,ii) =='T'
st = st + 1;
elseif II(1,ii) == 'u'| II(1,ii) =='U'
su = su + 1;
elseif II(1,ii) == 'v'| II(1,ii) =='V'
sv = sv + 1;
elseif II(1,ii) == 'w'| II(1,ii) =='W'
sw = sw + 1;
elseif II(1,ii) == 'x'| II(1,ii) =='X'
sx = sx + 1;
elseif II(1,ii) == 'y'| II(1,ii) =='Y'
sy = sy + 1;
elseif II(1,ii) == 'z'| II(1,ii) =='Z'
sz = sz + 1;
end
end
sa;
sb;
sc;
sd;
se;
sf;
sg;
sh;
si;
sj;
sk;
sl;
sm;
sn;
so;
sp;
sq;
sr;
ss;
st;
su;
sv;
sw;
sx;
sy;
sz;
[vowels] = sa+se+si+so+su;
letter = input('Enter letter you would like to know the amount of','s')
if letter == 'a' | letter == 'A'
sa
elseif letter == 'b' | letter == 'B'
sb
elseif letter == 'c' | letter == 'C'
sc
elseif letter == 'd' | letter == 'D'
sd
elseif letter == 'e' | letter == 'E'
se
elseif letter == 'f' | letter == 'F'
sf
elseif letter == 'g' | letter == 'G'
sg
elseif letter == 'h' | letter == 'H'
sh
elseif letter == 'i' | letter == 'I'
si
elseif letter == 'j' | letter == 'J'
sj
elseif letter == 'k' | letter == 'K'
sk
elseif letter == 'l' | letter == 'L'
sl
elseif letter == 'm' | letter == 'M'
sm
elseif letter == 'n' | letter == 'N'
sn
elseif letter == 'o' | letter == 'O'
so
elseif letter == 'p' | letter == 'P'
sp
elseif letter == 'q' | letter == 'Q'
sq
elseif letter == 'r' | letter == 'R'
sr
elseif letter == 's' | letter == 'S'
ss
elseif letter == 't' | letter == 'T'
st
elseif letter == 'u' | letter == 'U'
su
elseif letter == 'v' | letter == 'V'
sv
elseif letter == 'w' | letter == 'W'
sw
elseif letter == 'x' | letter == 'X'
sx
elseif letter == 'y' | letter == 'Y'
sy
else letter == 'z' | letter == 'Z'
sz
end
fprintf('The number of vowels are')
vowels
Here is the subroutine for counting the vowels which I think works fine:
function [vowels] = vowels;
vowels = sa+se+si+so+su
end
Finally, the subroutine for counting the letters which stops at the input:
function [II] = II
II = input('Please enter your quote','s')
[n,m] = size(II);
sa = 0;
sb = 0;
sc = 0;
sd = 0;
se = 0;
sf = 0;
sg = 0;
sh = 0;
si = 0;
sj = 0;
sk = 0;
sl = 0;
sm = 0;
sn = 0;
so = 0;
sp = 0;
sq = 0;
sr = 0;
ss = 0;
st = 0;
su = 0;
sv = 0;
sw = 0;
sx = 0;
sy = 0;
sz = 0;
for ii = 1:m
if II(1,ii) == 'a' | II(1,ii) =='A'
sa = sa + 1;
elseif II(1,ii) == 'b'| II(1,ii) =='B'
sb = sb + 1;
elseif II(1,ii) == 'c'| II(1,ii) =='C'
sc = sc + 1;
elseif II(1,ii) == 'd'| II(1,ii) =='D'
sd = sd + 1;
elseif II(1,ii) == 'e'| II(1,ii) =='E'
se = se + 1;
elseif II(1,ii) == 'f'| II(1,ii) =='F'
sf = sf + 1;
elseif II(1,ii) == 'g'| II(1,ii) =='G'
sg = sg + 1;
elseif II(1,ii) == 'h'| II(1,ii) =='H'
sh = sh + 1;
elseif II(1,ii) == 'i'| II(1,ii) =='I'
si = si + 1;
elseif II(1,ii) == 'j'| II(1,ii) =='J'
sj = sj + 1;
elseif II(1,ii) == 'k'| II(1,ii) =='K'
sk = sk + 1;
elseif II(1,ii) == 'l'| II(1,ii) =='L'
sl = sl + 1;
elseif II(1,ii) == 'm'| II(1,ii) =='M'
sm = sm + 1;
elseif II(1,ii) == 'n'| II(1,ii) =='N'
sn = sn + 1;
elseif II(1,ii) == 'o'| II(1,ii) =='O'
so = so + 1;
elseif II(1,ii) == 'p'| II(1,ii) =='P'
sp = sp + 1;
elseif II(1,ii) == 'q'| II(1,ii) =='Q'
sq= sq + 1;
elseif II(1,ii) == 'r'| II(1,ii) =='R'
sr = sr + 1;
elseif II(1,ii) == 's'| II(1,ii) =='S'
ss= ss + 1;
elseif II(1,ii) == 't'| II(1,ii) =='T'
st = st + 1;
elseif II(1,ii) == 'u'| II(1,ii) =='U'
su = su + 1;
elseif II(1,ii) == 'v'| II(1,ii) =='V'
sv = sv + 1;
elseif II(1,ii) == 'w'| II(1,ii) =='W'
sw = sw + 1;
elseif II(1,ii) == 'x'| II(1,ii) =='X'
sx = sx + 1;
elseif II(1,ii) == 'y'| II(1,ii) =='Y'
sy = sy + 1;
elseif II(1,ii) == 'z'| II(1,ii) =='Z'
sz = sz + 1;
end
end
sa;
sb;
sc;
sd;
se;
sf;
sg;
sh;
si;
sj;
sk;
sl;
sm;
sn;
so;
sp;
sq;
sr;
ss;
st;
su;
sv;
sw;
sx;
sy;
sz;
letter = input("Enter letter you would like to know the amount of","s")
if letter == 'a' | letter == 'A'
sa
elseif letter == 'b' | letter == 'B'
sb
elseif letter == 'c' | letter == 'C'
sc
elseif letter == 'd' | letter == 'D'
sd
elseif letter == 'e' | letter == 'E'
se
elseif letter == 'f' | letter == 'F'
sf
elseif letter == 'g' | letter == 'G'
sg
elseif letter == 'h' | letter == 'H'
sh
elseif letter == 'i' | letter == 'I'
si
elseif letter == 'j' | letter == 'J'
sj
elseif letter == 'k' | letter == 'K'
sk
elseif letter == 'l' | letter == 'L'
sl
elseif letter == 'm' | letter == 'M'
sm
elseif letter == 'n' | letter == 'N'
sn
elseif letter == 'o' | letter == 'O'
so
elseif letter == 'p' | letter == 'P'
sp
elseif letter == 'q' | letter == 'Q'
sq
elseif letter == 'r' | letter == 'R'
sr
elseif letter == 's' | letter == 'S'
ss
elseif letter == 't' | letter == 'T'
st
elseif letter == 'u' | letter == 'U'
su
elseif letter == 'v' | letter == 'V'
sv
elseif letter == 'w' | letter == 'W'
sw
elseif letter == 'x' | letter == 'X'
sx
elseif letter == 'y' | letter == 'Y'
sy
else letter == 'z' | letter == 'Z'
sz
end
end

  6 Comments

Show 3 older comments
Justiz Baker
Justiz Baker on 22 Mar 2020
Icecream is good is the input. This is where the II subroutine stops. I did not set a breakpoint.
Sriram Tadavarty
Sriram Tadavarty on 22 Mar 2020
Clear the workspace and run only II in the command space, it will ask you in even to enter the letter. Did you get the same?
Justiz Baker
Justiz Baker on 22 Mar 2020
I need it to run in the script

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 22 Mar 2020
I advise you not to create 26 individual variables and replicate almost exactly the same code 26 times for each operation you want to perform.
Instead, I'd convert the text data into numbers ('A' and 'a' to 1, 'B' and 'b' to 2, ... 'Z' and 'z' to 26 or perhaps 'A' to 65, 'B' to 66, etc.) and use those as indices into a vector.
Since this sounds like it's probably homework I'll just point you to a couple of potentially useful functions. See the "Determine Type and Properties" and "Edit" sections on this documentation page.

  0 Comments

Sign in to comment.


Image Analyst
Image Analyst on 22 Mar 2020
Hint:
s = 'ice cream is good'
% Find out which elements are letters.
letterIndexes = isstrprop(s,'alpha')
% Extract letters only
s = s(letterIndexes)
% Convert to numbers
numbers = s - 'a' + 1
% Get counts of each letter and plot.
histogramObject = histogram(numbers, 27)
grid on;

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by