Converting name of month to number

40 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-1-29
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2019-1-28
[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 个评论
Adrij Roy
Adrij Roy 2019-1-29
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 2019-1-29
Sir I did with strcmp. The month names had a space at last so characters were not maching earlier.

请先登录,再进行评论。

更多回答(1 个)

Adam Danz
Adam Danz 2021-5-9
编辑:Adam Danz 2021-5-9
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 个评论
Walter Roberson
Walter Roberson 2021-5-9
This appears to use the Finance Toolbox
Adam Danz
Adam Danz 2021-5-9
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by