Convert a Matrix column to date

조회 수: 6 (최근 30일)
Brady Ross
Brady Ross 2017년 11월 15일
답변: KL 2017년 11월 15일
I have a matrix 5556x2 double which both columns are read as numbers. I want to convert to first column to be read as dates. the format is yyyyMMdd as in, 20010130. My code for a loop is here that works but in the final product I want the entire first column to be read as dates rather than numbers so when I plot the two columns the X axis is understandable.
len2 = 0;
for i = 1:16
year = i+2001;
filename =['CRND0103-',sprintf('%4.4d',year),'-AK_Barrow_4_ENE.txt'];
formatSpec = '%*5s%9f%*7*s%*8*s%*8*s%*8*s%*8f%8f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'ReturnOnError', false);
fclose(fileID);
bar = [dataArray{1:end-1}];
len1 = length(bar);
barrow(len2+1:len1+len2,:)= bar(:,:);
len2 = length(barrow);
end;
barrow(barrow == - 9999) = NaN;
  댓글 수: 1
Rik
Rik 2017년 11월 15일
If you use datenum as x values, the xticks should automatically show dates. Is that what you are looking for? I'm a little confused about how your text relates to the code.

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

채택된 답변

KL
KL 2017년 11월 15일
Try something like this,
dt = [20110130; 20110131; 20110201]; %your column
d = datetime(num2str(dt),'InputFormat','yyyyMMdd')

추가 답변 (0개)

카테고리

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