Extracting data from txt file

2 ビュー (過去 30 日間)
Marijn
Marijn 2013 年 4 月 17 日
編集済み: Azzi Abdelmalek 2013 年 12 月 8 日
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 Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by