Converting an entire MATLAB table column from epoch to datetime format

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개)

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

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
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;

댓글을 달려면 로그인하십시오.

%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);

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

R
R
2022년 2월 14일

댓글:

2022년 2월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by