Converting name of month to number

37 ビュー (過去 30 日間)
Adrij Roy
Adrij Roy 2019 年 1 月 28 日
編集済み: Adam Danz 2021 年 5 月 9 日
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
Sarah Crimi
Sarah Crimi 2019 年 2 月 1 日
Oh yes, you are right!

サインインしてコメントする。

採用された回答

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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by