Extracting data from txt file
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!