Converting name of month to number

조회 수: 41 (최근 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 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