slitting a number into year, mont and day

조회 수: 2 (최근 30일)
Johannes Deelstra
Johannes Deelstra 2013년 4월 19일
I have a column(a(:,1)) containing numbers like;
19900101
19900102
19900103
19900104
19900105
19900106
19900107
19900108
19900109
19900110
The numbers represent year(1:4), month(5:6) and daynumber(7:8).
How to I split these numbers into year, number of month and daynumber?
Thanks
Johannes

답변 (2개)

Walter Roberson
Walter Roberson 2013년 4월 19일
years = floor( a(:,1) ./ 10000);
months = mod( floor( a(:,1) ./ 100 ), 100);
days = mod( a(:,1), 100 );
  댓글 수: 1
Jan
Jan 2013년 4월 22일
I'd prefer this method, because it does not convert the numbers to strings and back to numbers again.

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


Daniel Shub
Daniel Shub 2013년 4월 19일
It is not clear what your "a" is. If it is a numeric array
a = [ 19900101
19900102
19900103
19900104
19900105
19900106
19900107
19900108
19900109
19900110
];
you need to convert it to a char array
a = int2str(a);
Obviously if it is already a char array, you do not need to convert it. You then need to grab the sections you want, and possibly convert them back into numbers ...
year = str2num(a(:, 1:4));
month = str2num(a(:, 5:6));
day = str2num(a(:, 7:8));
  댓글 수: 2
Johannes Deelstra
Johannes Deelstra 2013년 4월 22일
Thanks very much Daniel and Walter, useful and very effective.
Walter Roberson
Walter Roberson 2013년 4월 22일
If the problem is solved, please Accept an Answer.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by