Error in datetime array format for writing new matrix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Anak Agung Adhi Dermawan
le 14 Sep 2022
Modifié(e) : Stephen23
le 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 commentaires
dpb
le 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.
Réponse acceptée
Stephen23
le 14 Sep 2022
Modifié(e) : Stephen23
le 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 commentaires
Stephen23
le 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Calendar dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!