Switch problem

2 次查看(过去 30 天)
Benaiah
Benaiah 2011-7-30
Hello, I'm currently learning MATLAB, and the code from one question of the week 5 lab doesn't work. It returns me the error "??? SWITCH expression must be a scalar or string constant."
The code is below:
names = [{'Sam','Jim','Tom','Joe','Kai','Zoe','Jan','Pam','Bob','Ann'}];
Names = sort(names);
MarkUpdater = menu('please select a name from the menu: ',Names)
for i=1: length(Names)
switch Names
case 1
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markAnn = mark-1;
case 2
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markBob = mark-1;
case 3
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markJan = mark-1;
case 4
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markJim = mark-1;
case 5
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markJoe = mark-1;
case 6
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markKai = mark-1;
case 7
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markPam = mark-1;
case 8
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markSam = mark-1;
case 9
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markTom = mark-1;
case 10
mark=menu('please select the mark: ', ...
'0','1','2','3','4','5','6','7','8','9','10')
markZoe = mark-1;
end
end

采纳的回答

Walter Roberson
Walter Roberson 2011-7-30
Instead of switching on Names, you need to switch on MarkUpdater since that is your numeric value.
(There are, of course, much better ways to write that code, but presumably the lab will lead you through them.)
  2 个评论
Benaiah
Benaiah 2011-7-30
When i changed "switch Names" to "switch MarkUpdater", it just repeated loading the mark selection menu for the name i selected 10 times, then popped up an error saying "??? Undefined function or variable 'MarkBob' if i selected Ann as the name.
Oleg Komarov
Oleg Komarov 2011-7-30
Without knowing what are you trying to achieve is hard to guess.

请先登录,再进行评论。

更多回答(1 个)

Benaiah
Benaiah 2011-8-3
What you suggested Walter worked fine. All i had to do was move the MarkUpdater code line into the for loop, then it worked.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by