Extracting data from txt file

2 views (last 30 days)
Marijn
Marijn on 17 Apr 2013
Edited: Azzi Abdelmalek on 8 Dec 2013
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

Accepted Answer

Cedric
Cedric on 17 Apr 2013
Edited: Cedric on 18 Apr 2013
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!