how to split a cell into a year and a month?
2 views (last 30 days)
Show older comments
Tamar Pichkhadze
on 27 Aug 2015
Commented: Tamar Pichkhadze
on 27 Aug 2015
Hello to All, Could you please help me in splitting a cell, e.g. 192608, into 1926.08 or 1926/08? Such that I have a vector of date rows of the 'year.mm' format.
Thank you so much.
Cheers, Tamar
4 Comments
dpb
on 27 Aug 2015
That still doesn't answer the question precisely...they can still be either numeric or character strings.
Type
which Dates
presuming Dates is the variable name for the vector; otherwise, obviously, use the name of the variable you do have.
Accepted Answer
dpb
on 27 Aug 2015
>> c=192608; % numeric
>> datestr(datenum(fix(c/100),rem(c,100),1),'YYYY.mm')
ans =
1926.08
>> cc='192608'; % character string
>> datestr(datenum(cc,'YYYYmm'),'YYYY.mm')
ans =
1926.08
>>
0 Comments
More Answers (1)
Tamar Pichkhadze
on 27 Aug 2015
2 Comments
dpb
on 27 Aug 2015
"I typed in matlab editor:"
data = datenum(datestr(fix('Dates'), 'YYYY.mm'));
Ayup, that's got a problem; 'Dates' in the argument list is the character string, not the variable. Leave off the quotes.
You've also got the functions in wrong order; if the values are numeric to get the specific date string it would be
>> d=[192608; 192708]; % numeric vector two entries
>> ds=datestr(datenum(fix(d/100),rem(d,100),1),'YYYY.mm')
ds =
1926.08
1927.08
>>
The above turns the numeric value into year, month and assumed day 1 which are the minimum three arguments datenum needs for numeric inputs. datestr then takes that date number and formats it per the format requested as four-digit-year.two-digit-month
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!