how to split a cell into a year and a month?

2 views (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

dpb
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
>>

More Answers (1)

Tamar Pichkhadze
Tamar Pichkhadze on 27 Aug 2015
Hi dpb (sorry I dont't know your name), Thanks a lot for your quick reply!
Trying to implement your advice, but doesn't work for me as of now. So I typed in matlab editor: data = datenum(datestr(fix('Dates'), 'YYYY.mm')); where "Dates" is a column (a vector rather) of lots of rows with 192608, 192708 etc. I assume to be doing this incorrectly (completely new to MATLAB).
I appreciate your help a lot.
Cheers, Tamar
  2 Comments
dpb
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
Tamar Pichkhadze
Tamar Pichkhadze on 27 Aug 2015
dpb, it worked!! hurray! thank you so much. have a lovely rest of the day. Best regards, Tamar

Sign in to comment.

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!