Error in datetime array format for writing new matrix
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello matlab expert, I have this timeseries data in txt format. the first column contain year data in year decimal format. I want to convert the year decimal format into datetime and save it as new txt file but I got an error in writematrix because the datetime format. what function should I use?
clc;clear; close all;
format long g
filename1 = 'C:\ZTD\PWVctul.txt';
data1 = readmatrix(filename1);
T1 = array2table(data1);
tt = table2timetable(T1, 'RowTimes',datetime(0,1,1) + years(data1(:,1)));
tt4 = timetable2table (tt);
pw = table2array (tt4(1:end,"data12"));
time = table2array (tt4(1:end,"Time"));
newdata = [ty pw];
writematrix (newdata,'PWVctuldatetime.txt','Delimiter','space');
댓글 수: 4
dpb
2022년 9월 14일
I figured it had to be somesuch -- but didn't spend much time thinking about it nor about that years is the average duration animal...I was aware of that, just didn't think about it at the time.
Seems like a reasonable enhancement to the conversion types in datetime to build into it.
채택된 답변
Stephen23
2022년 9월 14일
편집: Stephen23
2022년 9월 14일
Ah, fractional years. Here is one way to convert a fractional year (yes, even leap years) to datetime:
format long G
M = readmatrix('PWVctul.txt')
X = M(:,2); % data
Y = M(:,1); % year
Z = datetime(floor(Y),1,1,'Format','y/MM/dd HH:mm:ss.SSSSSSSSS');
Z = Z + mod(Y,1).*((Z+calyears(1))-Z)
T = table(Z,X)
writetable(T,'newfile.txt') % no problem
댓글 수: 2
Stephen23
2022년 9월 14일
Another approach to converting from decimal years to datetime:
format long G
M = readmatrix('PWVctul.txt');
Y = M(:,1); % year
Z = datetime(floor(Y)+[0,1],1,1,'Format','y/MM/dd HH:mm:ss.SSSSSSSSS');
Z = Z(:,1) + mod(Y,1).*diff(Z,1,2)
... etc.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!