Error in datetime array format for writing new matrix

8 visualizaciones (últimos 30 días)
Anak Agung Adhi Dermawan
Anak Agung Adhi Dermawan el 14 de Sept. de 2022
Editada: Stephen23 el 15 de Sept. de 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 comentarios
dpb
dpb el 14 de Sept. de 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.
Stephen23
Stephen23 el 14 de Sept. de 2022
Editada: Stephen23 el 14 de Sept. de 2022
"Seems like a reasonable enhancement to the conversion types in datetime to build into it."
I asked about this on another thread, got this response:

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 14 de Sept. de 2022
Editada: Stephen23 el 14 de Sept. de 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')
M = 1831×2
1.0e+00 * 2014.99980974125 0.0627371458432432 2015.00236052519 0.0619196013484259 2015.00509549932 0.0583826355657704 2015.00783049723 0.0549544140538488 2015.01057022325 0.0497257245907533 2015.01330994928 0.0412166574719457 2015.01604967531 0.0460983177325905 2015.01878464955 0.0448768178251868 2015.02151964734 0.0450682606219937 2015.02425937337 0.0464028118802565
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)
Z = 1831×1 datetime array
2014/12/31 22:20:00.000057449 2015/01/01 20:40:41.522390028 2015/01/02 20:38:11.666557966 2015/01/03 20:35:42.560648069 2015/01/04 20:35:42.560410186 2015/01/05 20:35:42.560494974 2015/01/06 20:35:42.560572592 2015/01/07 20:33:12.708211031 2015/01/08 20:30:43.598515132 2015/01/09 20:30:43.598592750 2015/01/10 20:30:43.598677538 2015/01/11 20:30:43.598762327 2015/01/12 20:30:43.598524444 2015/01/13 20:28:13.749941713 2015/01/14 20:25:44.636782485 2015/01/15 20:25:44.636544602 2015/01/16 20:25:44.636629390 2015/01/17 20:25:44.636714179 2015/01/18 20:23:14.791594778 2015/01/19 20:20:45.674649549 2015/01/20 20:20:45.674734337 2015/01/21 20:20:45.674811954 2015/01/22 20:20:45.674896743 2015/01/23 20:18:15.833247843 2015/01/24 20:15:46.712832112 2015/01/25 20:15:46.712916901 2015/01/26 20:15:46.712686189 2015/01/27 20:15:46.712763806 2015/01/28 20:13:16.874900907 2015/01/29 20:10:47.751014676
T = table(Z,X)
T = 1831×2 table
Z X _____________________________ __________________ 2014/12/31 22:20:00.000057449 0.0627371458432432 2015/01/01 20:40:41.522390028 0.0619196013484259 2015/01/02 20:38:11.666557966 0.0583826355657704 2015/01/03 20:35:42.560648069 0.0549544140538488 2015/01/04 20:35:42.560410186 0.0497257245907533 2015/01/05 20:35:42.560494974 0.0412166574719457 2015/01/06 20:35:42.560572592 0.0460983177325905 2015/01/07 20:33:12.708211031 0.0448768178251868 2015/01/08 20:30:43.598515132 0.0450682606219937 2015/01/09 20:30:43.598592750 0.0464028118802565 2015/01/10 20:30:43.598677538 0.0451572386581995 2015/01/11 20:30:43.598762327 0.0523973221610946 2015/01/12 20:30:43.598524444 0.0631753909682948 2015/01/13 20:28:13.749941713 0.0593898128672308 2015/01/14 20:25:44.636782485 0.0557321130217992 2015/01/15 20:25:44.636544602 0.0555264085669299
writetable(T,'newfile.txt') % no problem
  2 comentarios
Stephen23
Stephen23 el 14 de Sept. de 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)
Z = 1831×1 datetime array
2014/12/31 22:20:00.000057449 2015/01/01 20:40:41.522390028 2015/01/02 20:38:11.666557966 2015/01/03 20:35:42.560648069 2015/01/04 20:35:42.560410186 2015/01/05 20:35:42.560494974 2015/01/06 20:35:42.560572592 2015/01/07 20:33:12.708211031 2015/01/08 20:30:43.598515132 2015/01/09 20:30:43.598592750 2015/01/10 20:30:43.598677538 2015/01/11 20:30:43.598762327 2015/01/12 20:30:43.598524444 2015/01/13 20:28:13.749941713 2015/01/14 20:25:44.636782485 2015/01/15 20:25:44.636544602 2015/01/16 20:25:44.636629390 2015/01/17 20:25:44.636714179 2015/01/18 20:23:14.791594778 2015/01/19 20:20:45.674649549 2015/01/20 20:20:45.674734337 2015/01/21 20:20:45.674811954 2015/01/22 20:20:45.674896743 2015/01/23 20:18:15.833247843 2015/01/24 20:15:46.712832112 2015/01/25 20:15:46.712916901 2015/01/26 20:15:46.712686189 2015/01/27 20:15:46.712763806 2015/01/28 20:13:16.874900907 2015/01/29 20:10:47.751014676
... etc.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by