How can I convert the number 1 into a date value 20150101 (yyyyMMdd)?

조회 수: 1 (최근 30일)
André Gadêlha
André Gadêlha 2017년 9월 26일
댓글: Andrei Bobrov 2017년 9월 27일
How can I convert A:
A = [ 1 2 3 4 5 ... 731]
into
[ 20150101 20150102 20150103 20150104 20150105 ... 20161231]

답변 (3개)

Jan
Jan 2017년 9월 26일
편집: Jan 2017년 9월 27일
This works but is slow (see comments):
A = [ 1 2 3 4 5 731];
num = datenum('31-Dec-2014') + A;
vec = sscanf(datestr(num, 'yyyymmdd ').', '%d')
or faster:
mat = datevec(datenum('31-Dec-2014') + A);
vec = mat * [10000; 100; 1; 0; 0; 0]
[EDITED]
dt = datetime(2015, 1, A, 'Format', 'uuuuMMdd')
>> 1×6 datetime array
20150101 20150102 20150103 20150104 20150105 20161231
This looks similar to the wanted output, but it cannot be converted to a double vector directly.
  댓글 수: 4
Guillaume
Guillaume 2017년 9월 27일
@Jan,
"This looks similar to the wanted output, but it cannot be converted to a double vector directly."
Of course, it can:
dt = datetime(2015, 1, A, 'Format', 'uuuuMMdd')
str2double(cellstr(dt))
By the way, I would recommend spelling fully parameter names so that newbies don't wonder what the 'F' option is.
@André
In Matlab, you can type doc something or help something to learn about something. It's the fastest way to learn about functions you don't know about. So, try:
doc tic
Jan
Jan 2017년 9월 27일
@Guillaume: I meant directly. The format 'uuuuMMdd' does not convert the internal storage of the values, but concerns the output to strings only. Then cellstr(dt) is a complicated conversion already. str2double means some work also, because the conversion from a string to a double is surprisingly complicated to consider exceptions and care for replying the double which is as near as possible to the value represented in the string.
I do not know, how the date and time values are store internally: As serial date numbers or as date vectors. For the last case year(dt)*10000+... would be a "direct" conversion. But if date numbers are used, even cellstr(dt) means 2 conversions already to obtain the numerical values for year, month and day, and to create a string in the uuuuMMdd format.

댓글을 달려면 로그인하십시오.


Peter Perkins
Peter Perkins 2017년 9월 27일
There's a datetime method for that:
>> A = (1:731)';
>> d = datetime(2015,1,A)
d =
6×1 datetime array
01-Jan-2015
02-Jan-2015
[snip]
30-Dec-2016
31-Dec-2016
>> ymd = yyyymmdd(d);
ymd =
20150101
20150102
[snip]
20161230
20161231
But ask yourself why you want that. You are most likely better off sticking with d, the datetime vector, unless you need text, in which case ...
>> string(d,'yyyMMdd')
ans =
731×1 string array
"20150101"
"20150102"
"20150103"
[snip]
... or unless you reaaly need those double values to hand off to some other function that only accepts dates in that form.
  댓글 수: 2
Jan
Jan 2017년 9월 27일
+1. yyyymmdd()? Wow, this does really match the OP's needs. I will include it in the runtime comparison later.

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2017년 9월 26일
편집: Andrei Bobrov 2017년 9월 26일
out = datetime([2015 01 01],'F','uuuuMMdd') + A -1
or
x = datevec(datetime([2015 01 01]) + A -1);
out = x(:,1:3)*[10000;100;1];

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by