Extracting data from txt file

2 visualizzazioni (ultimi 30 giorni)
Marijn
Marijn il 17 Apr 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

Risposta accettata

Cedric
Cedric il 17 Apr 2013
Modificato: Cedric il 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.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by