Switch problem

1 回表示 (過去 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 Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by