I need help with a switch case and it only comes up with an array of zeros

2 views (last 30 days)
Days_into_Month = zeros(n,1);
Month = char(Month(1:n,1));
for d = (1:n)'
switch Month
case {'Jan'}
Days_into_Month(d,1) = {Total_Days(d,1) + 6};
case {'Feb'}
Days_into_Month(d,1) = {Total_Days(d,1) - 24};
case {'Mar'}
Days_into_Month(d,1) = {Total_Days(d,1) - 53};
case {'Apr'}
Days_into_Month(d,1) = {Total_Days(d,1) - 84};
case {'May'}
Days_into_Month(d,1) = {Total_Days(d,1) - 114};
case {'Jun'}
Days_into_Month(d,1) = {Total_Days(d,1) - 145};
case {'Jul'}
Days_into_Month(d,1) = {Total_Days(d,1) - 175};
case {'Aug'}
Days_into_Month(d,1) = {Total_Days(d,1) - 206};
case {'Sep'}
Days_into_Month(d,1) = {Total_Days(d,1) - 237};
case {'Oct'}
Days_into_Month(d,1) = {Total_Days(d,1) - 267};
case {'Nov'}
Days_into_Month(d,1) = {Total_Days(d,1) - 298};
case {'Dec'}
Days_into_Month(d,1) = {Total_Days(d,1) - 328};
case {'Ja_'}
Days_into_Month(d,1) = {Total_Days(d,1) - 359};
Actual_Year = Actual_Year + 1;
end
end
I need to find the date for each month in this matrix.
Month =
Mar
Dec
Oct
Oct
May
Jul
Dec
But it only shows up with zeros
Days_into_Month =
0
0
0
0
0
0
0
My total days into the year is this and it starts from January 6th.
Total_Days =
61
344
296
289
132
195
352
Also n = 7

Answers (1)

Walter Roberson
Walter Roberson on 19 Oct 2015
You are constructing Month as a column array, not as a row vector.
  2 Comments
Walter Roberson
Walter Roberson on 19 Oct 2015
I am going to invoke my Mind Reading Toolbox here and say that you needlessly make your cell array Month into an n x 3 character array Month and that now you are comparing the entire n x 3 character array in a switch. If I am correct then you need to change
switch Month
to
switch Month(d,:)
and live with the consequences if any of the entries was not exactly the same length as the others.
However, what you should have done instead is not used char() and instead should have used
switch Month{d}

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!