Error in datetime array format for writing new matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Anak Agung Adhi Dermawan
am 14 Sep. 2022
Bearbeitet: Stephen23
am 15 Sep. 2022
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 Kommentare
dpb
am 14 Sep. 2022
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.
Akzeptierte Antwort
Stephen23
am 14 Sep. 2022
Bearbeitet: Stephen23
am 14 Sep. 2022
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 Kommentare
Stephen23
am 14 Sep. 2022
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!