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

5 vues (au cours des 30 derniers jours)
Cakil
Cakil le 5 Mai 2022
Commenté : Cakil le 6 Mai 2022
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

Réponse acceptée

Stephen23
Stephen23 le 5 Mai 2022
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 commentaire
Cakil
Cakil le 6 Mai 2022
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by