フィルターのクリア

converting abbreviation of months to numerical value

11 ビュー (過去 30 日間)
Ted McG
Ted McG 2020 年 10 月 20 日
コメント済み: Ameer Hamza 2020 年 10 月 20 日
I have a large 1244x1 cell that contains jan, feb, mar,...ect and repeats. Is there an easy way to convert these to their respective numerical value (jan = 1, feb = 2, ...).
Thanks!

回答 (3 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 20 日
Try something like this
C = {'jan', 'Mar', 'feb'}; % for example
M = month(cellfun(@(x) datetime(x, 'InputFormat', 'MMM'), C));
  2 件のコメント
Ted McG
Ted McG 2020 年 10 月 20 日
I am receiving the following error now:
Error using project (line 27)
Subscripting into a table using one subscript (as in t(i)) or three or more
subscripts (as in t(i,j,k)) is not supported. Always specify a row subscript
and a variable subscript, as in t(rows,vars).
Ameer Hamza
Ameer Hamza 2020 年 10 月 20 日
Is your data available as a table? Can you attach it as a .mat file?

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


Stephen23
Stephen23 2020 年 10 月 20 日
ismember makes this easy:
>> D = {'mar','apr','nov','may'}; % your data
>> C = {'jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'};
>> [~,X] = ismember(lower(D),C)
X =
3 4 11 5

Star Strider
Star Strider 2020 年 10 月 20 日
Another approach:
mnth_nr = @(mth) find(strcmpi(mth, {'jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'}));
This works with a single input:
Out = mnth_nr('May')
producing:
Out =
5
and can be vectorised using cellfun with a table:
T1 = cell2table({'Feb'; 'jul'; 'Dec'}) % Create Table To Test Code
Out2 = cellfun(@(x)mnth_nr(x), T1.Var1)
producing:
T1 =
3×1 table
Var1
_______
{'Feb'}
{'jul'}
{'Dec'}
Out2 =
2
7
12
.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by