Nested elseif statement syntax
4 次查看(过去 30 天)
显示 更早的评论
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
3 个评论
per isakson
2021-4-23
Indentation makes it easier to read the code
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
回答(2 个)
per isakson
2021-4-23
编辑:per isakson
2021-4-23
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 个评论
per isakson
2021-4-27
编辑:per isakson
2021-4-27
No, I just try to explain why your script doesn't work, e.g. return "e" for [3 3 0 0].
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] )
MultiTapTexting_( [2,2,0,0] )
MultiTapTexting_( [2,2,2,0] )
MultiTapTexting_( [2,2,2,2] )
% MultiTapTexting_( [2,0,2,0] )
MultiTapTexting_( [2,2,2.2,0] )
%%
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
2021-4-23
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 个评论
Walter Roberson
2021-4-23
%Nokia 5130c key map
keymap = {
{'.', ',', '''', '?', '!', '"', '1', '-', '(', ')', '@', '/', ':', '_'},
{'a', 'b', 'c', '2'},
{'d', 'e', 'f', '3'},
{'g', 'h', 'i', '4'},
{'j', 'k', 'l', '5'},
{'m', 'n', 'o', '6'},
{'p', 'q', 'r', 's', '7'},
{'t', 'u', 'v', '8'},
{'w', 'x', 'y', 'z', '9'},
{' ', '0', char(13) }, %char(13) is carriage return
}
The * character does not input characters in itself. On the first press it opens a list of punctuation characters, but you have to cursor to one of them and press OK . On the second press it opens a list of emoji, but again you have to cursor to one of them and press OK.
The # character does not input characters in itself. It cycles between shift mode. When you are at the beginning of the line, the default mode is upper-case first character and lower case remaining characters until the end of sentance. If you are at the beginning of a sentance, then the first # activates all-caps mode and the second # activates all-lowercase mode, and the third # goes back to upper-first mode. If you are not at the beginning of the sentance but are at the beginning of a word then (by default) you are in all-lower mode, and the first # activates upper-first mode and the second activates all-caps and the third activates all-lower (and cycle). If you are not at the beginning of a sentance or the beginning of a word, then the first # activates all caps-mode and the second activates all-lower mode, and cycle back to all-caps (in this situation, upper-first is not offered.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!