How to remove date and time columns after merging as date_time?

2 次查看(过去 30 天)
Hello,
I have a cell array of 24 datasets with almost 1 year time-series. I have date and time columns seperately. I merged them and created a date_time column.
I want to use the merged date_time column as my datetime column and remove the seperate date and time columns
dat_folder= % my directory
files = dir(fullfile(dat_folder,'*.dat'));
files = fullfile(dat_folder,{files.name});
n_files = numel(files);
table18 = cell(1,n_files);
for ii = 1:n_files
table18{ii} = readtimetable(files{ii});
end
for ii = 1:n_files % I have 24 files
table18{1,ii}.Day_Month_Year=table18{1,ii}.Day_Month_Year+years(2000)
table18{1,ii}.date_time=table18{1,ii}.Day_Month_Year+table18{1,ii}.Hour_Minute_Second
newtable18{1,ii} = table18{1,ii}(:,[6,2])
newtable18{1,ii}.date_time.Format='uuuu-MM-dd HH:mm:ss'
end
You can see the mat file in the attachment. I will glad to hear any comment!
Best,
Ezgi

采纳的回答

Stephen23
Stephen23 2022-5-5
You can use REMOVEVARS:
Your code would be clearer if you use a temporary variable, e.g.:
for ii = 1:n_files
tmp = table18{ii};
tmp.Day_Month_Year = tmp.Day_Month_Year+years(2000);
tmp.date_time = tmp.Day_Month_Year+tmp.Hour_Minute_Second;
tmp = tmp(:,[6,2]); % ???
tmp.date_time.Format = 'uuuu-MM-dd HH:mm:ss';
tmp = removevars(tmp,{'Day_Month_Year','Hour_Minute_Second'});
table18{ii} = tmp;
end
  1 个评论
Cakil
Cakil 2022-5-6
Thank you it is working. I am new to Matlab so my scripts are not very clean yet.
The only thing is I am converting timetable to table to remove the variable and then converting to the the timetable again.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by