Converting an entire MATLAB table column from epoch to datetime format

23 visualizaciones (últimos 30 días)
R
R el 14 de Feb. de 2022
Comentada: Seth Furman el 15 de Feb. de 2022
I am currently reading in two files, both of that consist of Epoch time. To use timetable in MATLAB, I am required to have the format of time as datetime. I am currently trying to convert an entire "time" column into datetime from epoch. However, when I try to use the suggested function I get the following error. This is my first time properly using MATLAB so any help would be appreciated! Thank you :)
Error using datetime (line 588)
Input data must be one numeric matrix when converting from a different date/time representation.
Error in DataAnalysis_Ver1 (line 13)
y = datetime(x, 'ConvertFrom','posixtime','TimeZone','Asia/Tokyo');
%reading and plotting the Ring Data
sleeponData = readtable('file1.csv');
sleeponTimestamp = sleeponData(:,"time");
sleeponTimestamparray = table2array(sleeponTimestamp);
sleeponSportRaw = table2array(sleeponData(:,"sportRaw"));
sleeponSportRaw = 5000*sleeponSportRaw;
%converting epoch time to datetime
rows = height(sleeponData);
for row = 1:rows
x = sleeponData(row,1);
y = datetime(x, 'ConvertFrom','posixtime','TimeZone','Asia/Tokyo');
sleeponData.time(row) = y;
end
%reading the pillow
data = importdata('file2.csv');
data(:,2) = [];
tabledata = array2table(data);
%get rid of the second column (contains messages)
%pillowTime = array2table(data(:,1));
%synchronizing the timestamp
%new = synchronize(sleeponData, data, 'secondly','linear');
new = synchronize(sleeponData, tabledata);
disp(new);

Respuestas (2)

Seth Furman
Seth Furman el 14 de Feb. de 2022
It looks like we just need to change
x = sleeponData(row,1);
to the following
x = sleeponData{row,1};
Note the use of curly braces-{} instead of parentheses-(). sleeponData is a table, so indexing with parentheses-() will return another table with the specified data, while curly braces-{} will return the data at the specified row and column indices.
See the following page in our documentation for more information.
If that does not fix the error, please attach an example file (for instance file1.csv) and/or include a concrete example of a timestamp you are trying to convert into a datetime.
  2 comentarios
R
R el 15 de Feb. de 2022
Thank you for your help! I used the curly braces but I still got an error.
i have attached the picture of the error when I ran the code
Seth Furman
Seth Furman el 15 de Feb. de 2022
Please see Walter's response. The loop is not needed in this case, only
y = datetime(sleeponData.time, 'ConvertFrom','posixtime','TimeZone','Asia/Tokyo');
sleeponData.time = y;

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 15 de Feb. de 2022
%reading and plotting the Ring Data
sleeponData = readtable('file1.csv');
sleeponTimestamp = sleeponData(:,"time");
sleeponTimestamparray = table2array(sleeponTimestamp);
sleeponSportRaw = table2array(sleeponData(:,"sportRaw"));
sleeponSportRaw = 5000*sleeponSportRaw;
%converting epoch time to datetime
%no loop needed
y = datetime(sleeponData.time, 'ConvertFrom','posixtime','TimeZone','Asia/Tokyo');
sleeponData.time = y;
%reading the pillow
data = importdata('file2.csv');
data(:,2) = [];
tabledata = array2table(data);
%get rid of the second column (contains messages)
%pillowTime = array2table(data(:,1));
%synchronizing the timestamp
%new = synchronize(sleeponData, data, 'secondly','linear');
new = synchronize(sleeponData, tabledata);
disp(new);

Categorías

Más información sobre Data Type Conversion 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