Problem with formatting operators - textscan
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to bring in some data from a .csv file, but I can't seem to get the formatting operators correct when using textscan(). I'm sure this problem is trivial for someone with more experience. My data is all on one line separated by commas. Format:
XX mins YYYY-MM-DD hh:mm:ss, XX mins YYYY-MM-DD hh:mm:ss, XX mins YYYY-MM-DD hh:mm:ss,
Example data:
26 mins 2013-09-17 09:01:45, 29 mins 2013-09-18 16:53:12, 33 mins 2013-09-19 18:24:27,
After using fopen to open the file, I am trying to use textscan:
f = fopen('data.csv');
c = textscan(f,????,'Delimiter',',');
I've tried experimenting with different formatting operators at ????, but can't get anything that makes sense. I would like to extract the following as integers:
XX, YYYY, MM, DD, hh, mm, ss
Help?
0 Kommentare
Antworten (1)
Stephen23
am 8 Apr. 2025
Bearbeitet: Stephen23
am 8 Apr. 2025
txt = '26 mins 2013-09-17 09:01:45, 29 mins 2013-09-18 16:53:12, 33 mins 2013-09-19 18:24:27,';
Directly obtaining the integers you requested:
tmp = textscan(txt,'%f %*[^ ]%f%f%f%f%f%f', 'EndOfLine',',', 'Delimiter',{'-',':'}, 'CollectOutput',true)
tmp{1}
Or an alternative solution using DATETIME for the timestamp:
tmp = textscan(txt,'%f%*s%D%T', 'EndOfLine',',')
vec = tmp{1}
dt = tmp{2}+tmp{3};
dt.Format = 'yyyy-MM-dd HH:mm:ss'
dt.Year
dt.Month
% etc...
dt.Second
0 Kommentare
Siehe auch
Kategorien
Mehr zu Standard File Formats finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!