フィルターのクリア

How to read information from a txt file header?

1 回表示 (過去 30 日間)
Liqing
Liqing 2011 年 3 月 21 日
I have several hundred txt files with the below format.
http://cid-533c6ad99f5ed0c5.office.live.com/self.aspx/.Public/data2.txt
Is there a way that I can not only read the numeric data, but also the Date, Time, Latitude, and Longitude from the header automatically?
Thank you.

採用された回答

Matt Tearle
Matt Tearle 2011 年 3 月 21 日
Assuming the header information is always the same format (or at least same number of lines):
fid = fopen('data2.txt','rt');
tmp = textscan(fid,'%*s = %f',5,'headerlines',9);
metainfo = tmp{1}
hdrs = textscan(fid,repmat('%s',1,8),1,'delimiter',',');
units = textscan(fid,repmat('%s',1,8),1,'delimiter',',');
tmp = textscan(fid,repmat('%f',1,8),'delimiter',',','collectoutput',true);
data = tmp{1}
fclose(fid);
.
.
.
I'm hesitant to even suggest this, but... you could replace the third line with:
for k=1:size(tmp{1},1)
eval([tmp{1}{k},' = tmp{2}(k);']);
end
This will give you variables DATE, LATITUDE, etc with the values read from the file.
And may Cleve have mercy on my soul.
  2 件のコメント
Liqing
Liqing 2011 年 3 月 21 日
Hi Matt,
This is amazing!
Thank you so much for the big help.
Liqing
Matt Tearle
Matt Tearle 2011 年 3 月 22 日
Glad it worked for you! Let me know if you need help deciphering any of the syntax :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by