Clear Filters
Clear Filters

Switch problem

2 views (last 30 days)
Benaiah
Benaiah on 30 Jul 2011
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

Accepted Answer

Walter Roberson
Walter Roberson on 30 Jul 2011
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 Comments
Benaiah
Benaiah on 30 Jul 2011
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 on 30 Jul 2011
Without knowing what are you trying to achieve is hard to guess.

Sign in to comment.

More Answers (1)

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

Community Treasure Hunt

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

Start Hunting!