Converting an entire MATLAB table column from epoch to datetime format

17 次查看(过去 30 天)
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);

回答(2 个)

Seth Furman
Seth Furman 2022-2-14
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 个评论
R
R 2022-2-15
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 2022-2-15
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;

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2022-2-15
%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);

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by