reading specific parts of txt file

4 ビュー (過去 30 日間)
sermet
sermet 2015 年 8 月 22 日
編集済み: Walter Roberson 2015 年 8 月 22 日
[FileName,PathName] = uigetfile('*txt','Select the ionex file');
if FileName ~= 0
completename = fullfile(PathName, FileName);
data_2=fopen(completename);
else
return
end
head_lines = 0;
while 1
head_lines = head_lines+1;
line = fgets(data_2); %headers
answer = strfind(line,'EPOCH OF CURRENT MAP');
if ~isempty(answer)
break;
end
end
lines_to_skip_from_header=head_lines;
head_lines_2=0; % the other remaining lines after headers
while 1
head_lines_2 = head_lines_2+1;
line_2 = fgets(data_2); %data parts
answer_2 = strfind(line_2,'END OF DATA');
if ~isempty(answer_2)
break;
end
end
user_latitude=84.44;
user_longitude=56.65;
in txt file, "EPOCH OF CURRENT MAP" is the time of the data (which is 2 in trial.txt); LAT is latitude, LON1 and LON2 are first longitude and last longitude with 5 degrees incremental for each LAT/LON1/LON2/DLON/H. Numbers between LAT/LON1/LON2/DLON/H are data.
I need to write codes which find the two nearest LAT and LON in txt file w.r.t. the user's lat and lon for each "EPOCH OF CURRENT MAP" time.
for example, for "user_latitude=84.44" and "user_longitude=56.65"; two nearest LAT are 85, 82.50; two nearest LON are 55, 60 and four data of first "EPOCH OF CURRENT MAP" are 141 for LAT 82.50, LON 55, 147 for LAT 82.50, LON 60, 170 for LAT 85, LON 55, 174 for LAT 85, LON 60.
I tried to read with fgets function but only last line of data remains.
Thanks in advance.
  3 件のコメント
dpb
dpb 2015 年 8 月 22 日
Oh, if TMW had just not thought they had to re: C i/o and its abomination for formatting strings in lieu of FORMAT!!!
For OP, I'd suggest just reading the whole file and then selecting the section from memory desired would be simpler than trying do the lookup on the fly.
The sections are regular-enough that writing a sequence to read one set is fairly straightforward, then wrap that in a loop. Then all that's left is the first header section that you've already found.
Use textscan and the actual format as the file is written to parse the actual numeric values for each section instead of reading the file line-by-line as a string; this will leave you with an array of values that you can then more easily search for nearest match.
BTW, use fgetl here instead of fgets; you don't want/need the \n character in memory; fgetl discards it.
Walter Roberson
Walter Roberson 2015 年 8 月 22 日
編集済み: Walter Roberson 2015 年 8 月 22 日
One trick for you: When you use textscan(), the format you specify does not need to cover an entire line at a time. You have blocks that appear to be 4 lines of 16 followed by 1 line of 9: you can code that as a '%f' format with a repeat count of 73.
However, some of your values are in fixed-width fields, and those are difficult to handle properly with textscan(). You might want to modify http://www.mathworks.com/matlabcentral/fileexchange/10866-fixed-width-import for this purpose.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by