Error in datetime array format for writing new matrix
3 ビュー (過去 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 で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!