Converting name of month to number

44 visualizzazioni (ultimi 30 giorni)
Adrij Roy
Adrij Roy il 28 Gen 2019
Modificato: Adam Danz il 9 Mag 2021
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 Commenti
Walter Roberson
Walter Roberson il 29 Gen 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
Sarah Crimi
Sarah Crimi il 1 Feb 2019
Oh yes, you are right!

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (1)

Adam Danz
Adam Danz il 9 Mag 2021
Modificato: Adam Danz il 9 Mag 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 Commenti
Walter Roberson
Walter Roberson il 9 Mag 2021
This appears to use the Finance Toolbox
Adam Danz
Adam Danz il 9 Mag 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.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by