Converting name of month to number

59 views (last 30 days)
I have a cellmatrix where there are two columns wth name of months. I want to replace the name of months by serial numbers from 1 to 12.
How can I do it in matlab 2016a?I am new to matlab & I tried using strcmp, strrep & even with switch.
Please suggest.
1)
if strcmp(z1(n,5),month(m,1))
z1{n,5} = strrep(z1{n,5},'z1{n,5}','m')
break;
else z1{n,5} = z1{n,5};
end
2)
str = z1{n,5};
switch (str)
case ('January')
z1{n,5} = 1;
.
.
.
case ('December')
z1{n,5} = 12;
end
None worked correctly.
  5 Comments
Walter Roberson
Walter Roberson on 29 Jan 2019
Sarah Crimi: strcmp() can use cell array of character vectors without needing to pull the entries out.
>> strcmp({'hello', 'sam'}, {'goodbye', 'sam'})
ans =
1×2 logical array
0 1

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jan 2019
[tf, idx] = ismember(z1(:,5), {'January', 'February', 'March'})
now tf(nn) is true if z1{nn,5} is matched and idx(nn) is the month number if tf(nn) is true. No loop needed.
  2 Comments
Adrij Roy
Adrij Roy on 29 Jan 2019
Sir this is showing error.
%% Converting Month into numbers from 1 to 12
month = {'January','February','March','April','May','June','July','August','September','October','November','December'}';
[tf,idx] = ismember(z1(:,5),{'January','February','March','April','May','June','July','August','September','October','November','December'});
%for n = 2:length(z1)
%M = {month(z1(:,5))};
%end
%if strcmp(z1(n,5),month(m,1))
%z1{n,5} = strrep(z1{n,5},'z1{n,5}','m')
%break;
%else z1{n,5} = z1{n,5};
%end
% C = z1{n,5};
%ff = find(strcmp(month(:,1),C));
%z1{n,5} = ff;
%Jan = strrep(z1(:,5),'January','1');
Error using cell/ismember (line 34)
Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a
string.
Error in Precip_crop_yield (line 51)
[tf,idx] =
ismember(z1(:,5),{'January','February','March','April','May','June','July','August','September','October','November','December'});
>>
Adrij Roy
Adrij Roy on 29 Jan 2019
Sir I did with strcmp. The month names had a space at last so characters were not maching earlier.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 9 May 2021
Edited: Adam Danz on 9 May 2021
Another way to convert month names to numbers that is quite flexible in ignoring case and accpeting month appreviations. Requires Finance Toolbox
monthNames = {'jan','March','october','Nov'}; % accepts string arrays, too
monthNum = month(monthNames,"mmmm")
monthNum = 1×4
1 3 10 11
Another option that does not require any toolbox but is not as quite as flexible since appreviated month names require a different format string.
% Full month names, not case senstive
monthNames = {'March','May','june'};
month(datetime(monthNames,'InputFormat','MMMM')) % 4 M's
ans = 1×3
3 5 6
% Abbreviated month names (3 letters), not case sensitive
monthNamesAbrv = {'Jan','Oct','dec'};
month(datetime(monthNamesAbrv,'InputFormat','MMM')) % 3 M's
ans = 1×3
1 10 12
A safer version of the example above in cases where abbreviations are longer than 3 letters (ie, "Sept")
monthNamesAbrv = {'sept','oct','June'};
monthNamesAbrvClean = cellfun(@(str){str(1:3)},cellstr(monthNamesAbrv));
month(datetime(monthNamesAbrvClean,'InputFormat','MMM')) % 3 M's
ans = 1×3
9 10 6
  2 Comments
Walter Roberson
Walter Roberson on 9 May 2021
This appears to use the Finance Toolbox
Adam Danz
Adam Danz on 9 May 2021
Thanks WR. I often overlook dependencies for some toolbox functions I bump into without ever looking them up. I'll update my answer because I just found another way worth sharing, too.

Sign in to comment.

Categories

Find more on Characters and Strings 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!