Switch case and matrix input

1 Ansicht (letzte 30 Tage)
Nicholas Carlson
Nicholas Carlson am 18 Okt. 2015
Kommentiert: Stephen23 am 17 Okt. 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 Kommentare
FM
FM am 17 Okt. 2022
It sure would be helpful if the documentation specified that the switch expression must be a scalar.
Stephen23
Stephen23 am 17 Okt. 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."

Melden Sie sich an, um zu kommentieren.

Antworten (1)

the cyclist
the cyclist am 18 Okt. 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.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by