MATLAB: How to define a new variable in a table, Part 2

MATLAB

Suppose I have a table 'test' such as
month
——-
January
February
I now want to define a string variable 'season' such that
'season'='winter' if 'month' is 'January', 'February', or 'March'
etc. The output would be
month season
—- —
January winter
February winter
Please advise.

Best Answer

Try this
T = % you table
Months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'}.';
Mnth2Season = [1 1 1 2 2 2 3 3 3 4 4 4].'; % for example
Seasons = {'Winter', 'Spring', 'Summer', 'Autumn'}.';
[~, idx] = ismember(T.Months, Months);
T.Season = Seasons(Mnth2Season(idx));