Switch case and matrix input

1 view (last 30 days)
Nicholas Carlson
Nicholas Carlson on 18 Oct 2015
Commented: Stephen23 on 17 Oct 2022
n = input('number of rows \n ')
for d = (1:n)
Month = {'Mar'}
Days = cell(n,1);
switch Month
case {'Jan'}
Days = Total_Days(d,1) +6;
case {'Feb'}
Days = Total_Days(d,1) - 24;
case {'Mar'}
Days = Total_Days(d,1) - 53;
case {'Apr'}
Days = Total_Days(1:end,1) - 84;
case {'May'}
Days = Total_Days(1:end,1) - 114;
case {'Jun'}
Days = Total_Days(1:end,1) - 145;
case {'Jul'}
Days = Total_Days(1:end,1) - 175;
case {'Aug'}
Days = Total_Days(1:end,1) - 206;
case {'Sep'}
Days = Total_Days(1:end,1) - 237;
case {'Oct'}
Days = Total_Days(1:end,1) - 267;
case {'Nov'}
Days = Total_Days(1:end,1) - 298;
case {'Dec'}
Days = Total_Days(1:end,1) - 328;
case {'Ja_'}
Days = Total_Days(1:end,1) - 359;
Actual_Year = Actual_Year + 1;
end
end
My code runs with an error shown like this.
SWITCH expression must be a scalar or string constant.
Error in Setting_Date_New (line 33)
switch Month
I need the answer to be in a matrix form that can compute with multiple sets of months at a time.
  2 Comments
FM
FM on 17 Oct 2022
It sure would be helpful if the documentation specified that the switch expression must be a scalar.
Stephen23
Stephen23 on 17 Oct 2022
"It sure would be helpful if the documentation specified that the switch expression must be a scalar."
It does. The SWITCH documentation states "An evaluated switch_expression must be a scalar or character vector. An evaluated case_expression must be a scalar, a character vector, or a cell array of scalars or character vectors."

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 18 Oct 2015
It's not clear to me what you are trying to do, but if you replace
Month = {'Mar'}
with
Month = 'Mar'
you will at least get past your current error.

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!