how to read selected data from text file matlab ?

3 vues (au cours des 30 derniers jours)
pruth
pruth le 16 Août 2018
Modifié(e) : pruth le 17 Août 2018
hi, I have this text file. (attached).
from this text file I only want read only second column(date), third column(time) and the last column. first 11 rows can be deleted. since time and date are two different column I am not able to combine them and make single vector. I just want do hourly average which I can do for sure. but I can not read and collect the data from this file in the first place. I Hope you understand my question. thanks

Réponse acceptée

jonas
jonas le 16 Août 2018
Modifié(e) : jonas le 16 Août 2018
readtable can do this for you easily.
%%Read the relevant columns, exclude 10 rows
opts=detectImportOptions('str.txt','NumHeaderLines',10);
opts.SelectedVariableNames = {'timestamp','RPM','absE_f'};
opts = setvartype(opts,{'timestamp','RPM'},{'datetime','duration'})
T=readtable('str.txt',opts)
t=T.timestamp+T.RPM;
readtable automatically detects the datetime and duration format of columns 2 and 3. If it fails, you can specify the format in detectImportOptions. Now, just build a timetable:
TT=timetable(t,T.absE_f)
ans =
10×1 timetable
t Var1
____________________ ________
02-Jan-2006 00:00:00 131.96
02-Jan-2006 00:00:10 -0.26753
02-Jan-2006 00:00:20 -0.29573
02-Jan-2006 00:00:30 127.73
Finally,
TT2 = retime(TT,'hourly','mean')
  16 commentaires
pruth
pruth le 17 Août 2018
thank you so much , Jonas, for your time and patience !!
jonas
jonas le 17 Août 2018
Yes sorry, my bad. Grab the time by
datenum(TT.t)
No problem! Happy to help.

Connectez-vous pour commenter.

Plus de réponses (1)

pruth
pruth le 17 Août 2018
Modifié(e) : pruth le 17 Août 2018
final code. !!
clear all
close all
clc
opts=detectImportOptions('2jan.txt','NumHeaderLines',10); %%%Read the relevant columns, exclude 10 rows
opts.SelectedVariableNames = {'timestamp','RPM','absE_f'};
opts = setvartype(opts,'timestamp','datetime');
opts = setvartype(opts,'RPM','datetime');
opts = setvaropts(opts,'timestamp','InputFormat','MM/dd/yyyy');
opts = setvaropts(opts,'RPM','InputFormat','HH:mm:SS');
T=readtable('2jan.txt',opts);
Date=datetime(T.timestamp,'format','yyyy-MM-dd hh:mm:SS');
TimeOfDay=hours(hour(T.RPM))+minutes(minute(T.RPM))+seconds(second(T.RPM));
t=Date+TimeOfDay;
TT=timetable(t,T.absE_f);
TT2 = retime(TT,'hourly','mean');
final(:,1)=datenum(TT2.t(:,1));
final(:,2)= TT2.Var1(:,1);
  2 commentaires
jonas
jonas le 17 Août 2018
Please don't flag posts as it alerts the mods!
pruth
pruth le 17 Août 2018
oh fine. unflagged :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by