How to extract coordinate from the header files?
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone.
I have 6 headerlines and then data. I want to extract the only the coordinate numbers in the first headerline.
Example data as below and also as attached
Lat: 6.4931, Lon: 99.6067, Parameter: z(m)
Depth (m): 22.45
Constituents included: m2 s2 k1 o1
Time start: 00:00, 1. 1.1993
Time step (min): 60.00
Time Series length (hours):236664
01-Jan-1993 00:00:00 -0.0946
01-Jan-1993 01:00:00 -0.3369
01-Jan-1993 02:00:00 -0.5110
01-Jan-1993 03:00:00 -0.5776
Please see the script below:
clc;clear all; close all;
delim = ' '; % space delimited
% delim = '\t'; % tab delimited
% delim = ','; % comma delimited
%Input File
fid=fopen('aa__file002.txt');
% A = textscan(fid,'%s %n','Delimiter',delim); % read file;
A = textscan(fid,'%s %s %f','HeaderLines',6); % read file;
Headerlin1 = textscan(fid,'%s %f%s %s %f%s %s %s',1,'Delimiter','delim');
fclose(fid); % close file
Any help regarding the problem are really appreciated.
0 个评论
采纳的回答
Cris LaPierre
2020-9-2
Think of fid as a pointer that tells textscan where to read from. It moves its way byte by byte through the file until it reaches the end. When you call textscan for Headerlin1, it's at the end of the file. Your option, then, is to either read in Headerlin1 first, and then continue with reading in the rest of the file, or use frewind to bring fid back to the beginning of the file before reading in the first line.
You can get textscan to skip the text before a number by placing that text right before the format spec: Lat:%f
Option 1
fid=fopen('aa__file002.txt');
Headerlin1 = textscan(fid,'Lat:%f Lon:%f %*[^\n]','Delimiter',','); % extra bit reads to the end of the line
A = textscan(fid,'%s %s %f','HeaderLines',5); % read file;
fclose(fid);
Option 2
fid=fopen('aa__file002.txt');
A = textscan(fid,'%s %s %f','HeaderLines',6); % read file;
frewind(fid);
Headerlin1 = textscan(fid,'Lat:%f Lon:%f','Delimiter',',');
fclose(fid);
2 个评论
Cris LaPierre
2020-9-2
编辑:Cris LaPierre
2020-9-6
For easier handling of dates and time, consider reading in the date as a datetime, and the time as a duration.
A = textscan(fid,'%{dd-MMM-yyyy}D %{hh:mm:ss}T %f','HeaderLines',5); % read file;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!