# Nested elseif statement syntax

5 views (last 30 days)
Benjamin Weir on 23 Apr 2021
Edited: per isakson on 27 Apr 2021
Hi,
I've been working on a logic tree to work through repeating numbers as inputs where the outputs are alphabetical, similar to the Multi-tap texting pre-smartphone. (i.e. '2'=a, '2' '2' =b, '3' = d, '3' '3' '3' = f, and so on.
I tried using nested elseifs that seemed to work on their own, but paired with the rest of the elseif statements only the sequence for inputs of '9' (wxyz) is working. Does Anyone know what my error is here? I know this code is inefficient but I did my best for what is my first matlab project.
if inputs(1) == 0
disp('error, no input')
elseif inputs(1) == 9
if inputs(2) == 0
disp('w')
elseif inputs(2) == 9
if inputs(3) == 0
disp('x')
elseif inputs(3) == 9
if inputs(4) == 0
disp('y')
elseif inputs(4) == 9
disp('z')
elseif inputs(1) == 8
if inputs(2) == 0
disp('t')
elseif inputs(2) == 8
if inputs(3) == 0
disp('u')
elseif inputs(3) == 8
disp('v')
elseif inputs(1) == 7
if inputs(2) == 0
disp('p')
elseif inputs(2) == 7
if inputs(3) == 0
disp('q')
elseif inputs(3) == 7
if inputs(4) == 0
disp('r')
elseif inputs(4) == 7
disp('s')
elseif inputs(1) == 6
if inputs(2) == 0
disp('m')
elseif inputs(2) == 6
if inputs(3) == 0
disp('n')
elseif inputs(3) == 6
disp('o')
elseif inputs(1) == 5
if inputs(2) == 0
disp('j')
elseif inputs(2) == 5
if inputs(3) == 0
disp('k')
elseif inputs(3) == 5
disp('l')
elseif inputs(1) == 4
if inputs(2) == 0
disp('g')
elseif inputs(2) == 4
if inputs(3) == 0
disp('h')
elseif inputs(3) == 3
disp('i')
elseif inputs(1) == 3
if inputs(2) == 0
disp('d')
elseif inputs(2) == 3
if inputs(3) == 0
disp('e')
elseif inputs(3) == 3
disp('f')
elseif inputs(1) == 2
if inputs(2) == 0
disp('a')
elseif inputs(2) == 2
if inputs(3) == 0
disp('b')
elseif inputs(3) == 2
disp ('c')
else
disp('error, incorrect input')
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
Benjamin Weir on 26 Apr 2021
My inputs is a matrix determined by another program that will only return the first number it sees, followed by either the number repeating or a '0', and it is always inputs = [x x x x]. So for example, if the first number is '3', the program will always return inputs = [3 3 0 0], which should be a return of 'e'.
I have it set so that for even inputs = [3 3 3 3] would be the same as inputs = [3 3 3 0], because on a nokia esque phone there is no fourth character to cycle to, only 'd', 'e', and 'f', so both of these cases would be 'f'.
Therefore, i am just confused on how to get this code to work because it is pretty much ok for what i need.

per isakson on 23 Apr 2021
Edited: per isakson on 23 Apr 2021
There is no else block in the outer if-elseif-else-end. This code will display something only for inputs(1)==0 and for inputs(1)==9. Thus, "only the sequence for inputs of '9' (wxyz) is working."
if inputs(1) == 0
disp('error, no input')
elseif inputs(1) == 9
% a lot of nested if...end blocks
end
##### 2 CommentsShowHide 1 older comment
per isakson on 27 Apr 2021
No, I just try to explain why your script doesn't work, e.g. return "e" for [3 3 0 0].
Read carefully the documentation on if, elseif, else - Execute statements if condition is true.
Your script is logically flawed, there is no way it can return a letter for any "key" other than "9". The statement, elseif inputs(1) == 8, etc., must appear in the outer/top if-elseif-else-end block.
Below, I started on a new code. Currently, it only works for the key, "2".
%%
MultiTapTexting_( [2,0,0,0] )
ans = 'a'
MultiTapTexting_( [2,2,0,0] )
ans = 'b'
MultiTapTexting_( [2,2,2,0] )
ans = 'c'
MultiTapTexting_( [2,2,2,2] )
ans = 'c'
% MultiTapTexting_( [2,0,2,0] )
MultiTapTexting_( [2,2,2.2,0] )
Error using solution>MultiTapTexting_ (line 13)
incorrect input
%%
function out = MultiTapTexting_( inputs )
% Assert that the input value is legal
assert( ismember( inputs(1), (2:9) ) , 'incorrect input' )
assert( all(ismember( inputs(2:4), [0,(2:9)] ) ), 'incorrect input' )
assert( inputs(2)==0 || inputs(2)==inputs(1) , 'incorrect input' )
assert( inputs(3)==0 || inputs(3)==inputs(2) , 'incorrect input' )
assert( inputs(4)==0 || inputs(4)==inputs(3) , 'incorrect input' )
% The input value is legal, thus no more error checking needed
if inputs(1) == 2
if inputs(2) == 0
out = 'a';
else
if inputs(3) == 0
out = 'b';
else
out = 'c';
end
end
elseif inputs(1) == 3
elseif inputs(1) == 4
elseif inputs(1) == 5
elseif inputs(1) == 6
elseif inputs(1) == 7
elseif inputs(1) == 8
elseif inputs(1) == 9
end
end

Walter Roberson on 23 Apr 2021
keymap = {
{'1' 'punctuation'},
{'2', 'a', 'b', 'c'},
{'3', 'd', 'e', 'f'},
{'4', 'g', 'h', 'i'},
{'5', 'j', 'k', 'l'},
{'6', 'm', 'n', 'o'},
{'7', 'p', 'q', 'r', 's'},
{'8', 't', 'u', 'v'},
{'9', 'w', 'x', 'y', 'z'},
{'*', 'more punctuation'},
{'0'},
{'shift', '#'}
}
... Taken directly from an old Nokia phone on my shelf.
You start in the "beginning of letter" state. You get an input character. You take note of the character
Label "A": check: is the length of the cell associated with that character 1? If it is, emit the character and return to "beginning of letter" state.
If the length associated with the cell is not 1, then set a count to 1 and set the "current key" to the input, and set the "current character" to the first entry in the cell. Then enter "inside letter" state.
Label "Inside letter" state:
Try to get the next input. If end of input, then emit the "current character" and reset to "beginning of letter" state, and end
If the input key is different from the "current key", then emit the current character, and go back to "A"
If the input key is the same as the "current key" then set the count to (count mod length(cell associated with current key) + 1), and set the "current key" to the cell associated with "current key" indexed by the count. And go back to "inside letter" state
I don't know if my batteries on my old Nokia are completely dead or if I can fire it up long enough to determine the punctuation lists. I will leave it plugged into power for a bit.
##### 2 CommentsShowHide 1 older comment
Benjamin Weir on 26 Apr 2021
Hi Walter,
I am using inputs = [x x x x] from a program that is converting the DTMF tones into their numerical input. So my program has inputs = [3 3 0 0], this should be 'e' based on the Nokia-style system. This will only be working in a combination of the same numerical input and '0', so I am not working with anything other than 0-9. My follow-up question is, using the keymap you provided, how can my inputs matrix be applied? if i begin with the matrix already set, how can I have the inputs choose a character from the list?