Extracting data from txt file

1 次查看(过去 30 天)
I have a textfile that looks like this:
Site 4911445 PLUS at KG.LINTANG, PERAK
Date,Time,Flow m3/s
01/07/1960,06:00:00,17.81
01/07/1960,07:00:00,17.81
01/07/1960,08:00:00,17.81
01/07/1960,09:00:00,17.81
I'd like to create a matrix with in the first column the Date, second column the Time and third column the Flow. How to program that again?
fname = 'name.txt';
fid=fopen(fname,'r');
A = fscanf(fid, format)
What to use for the format?
Thanks heaps, Marijn

采纳的回答

Cedric
Cedric 2013-4-17
编辑:Cedric 2013-4-18
It is a good attempt that you made here with FOPEN and FSCANF; we can discuss the format if you want, but I would recommend using TEXTREAD in this case, which would simplify the process:
[dateString, timeString, flow] = textread('myFile.txt', '%s %s %f', ...
'delimiter', ',', 'headerlines', 2)
So here you have a one shot operation that opens/reads/formats the whole file, whereas you would have to read the file line by line and build arrays by yourself if you were using FSCANF.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by