Converting an entire MATLAB table column from epoch to datetime format

21 ビュー (過去 30 日間)
R
R 2022 年 2 月 14 日
コメント済み: Seth Furman 2022 年 2 月 15 日
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 Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by