MATLAB: Is there a more simple way to express this? How to simplify everything under “case 2”? Is there an alternative to writing out everything under case 2

MATLABswitch case

clear
clc
month = input('Enter numerical value of month');
switch month
case {9, 4, 6, 11}
days = 30;
case 2
answer = input('leap year (yes/no)', 's');
leapYear = (answer(1) == 'y');
if leapYear
days = 29;
else
days = 28;
end
case {1, 3, 5, 7, 8, 10, 12}
days = 31;
otherwise
error('bad month index')
end

Best Answer

Hi TSmith,
you can get rid of one redundant line by replacing
leapYear = (answer(1) == 'y');
if leapYear
with
if (answer(1) == 'y') % leap year
Otherwise it's good, readable code that does not cry out to be compressed in some tricky fashion that no one can figure out later.
{9, 4, 6, 11} could be reordered, though.