利用matlab提取文本间的数据
我正在尝试导出一组数据,对此我非常了解。所述数据具有以下结构:利用matlab提取文本间的数据,matlab,Matlab,我正在尝试导出一组数据,对此我非常了解。所述数据具有以下结构: # ************************************ # ***** GLOBAL ATTRIBUTES ****** # ************************************ # # PROJECT THEMIS # UT
# ************************************
# ***** GLOBAL ATTRIBUTES ******
# ************************************
#
# PROJECT THEMIS
#
UT UT BX_FGL-D BY_FGL-D BZ_FGL-D
(@_1_) (@_2_) (@_3_)
dd-mm-yyyy hh:mm:ss.mil.mic.nan.pic sec nT_GSE nT_GSE nT_GSE
21-05-2015 00:00:00.223.693.846.740 1.43208E+09 1.14132 9.14226 27.1446
21-05-2015 00:00:00.473.693.845.716 1.43208E+09 1.11194 9.16192 27.1798
21-05-2015 00:00:00.723.693.844.692 1.43208E+09 1.12992 9.11103 27.1595
21-05-2015 00:00:00.973.693.843.668 1.43208E+09 1.15966 9.15324 27.1589
21-05-2015 00:00:01.223.693.846.740 1.43208E+09 1.20576 9.14420 27.1388
21-05-2015 00:09:59.973.693.843.668 1.43208E+09 1.97445 8.66407 26.1837
#
# Key Parameter and Survey data (labels K0,K1,K2) are preliminary browse data.
# Generated by CDAWeb on: Mon May 27 06:01:29 2019
我要求将写在“dd-mm-yyyy…”和“##Key-Parameter”之间的内容导出到列中。
例如,第一行21-05-2015 00:00:00.223.693.846.740 1.43208E+09 1.14132 9.14226 27.1446必须出口到2152015、00,00,002369384740、1.43208E+09、1.14132、9.14226和27.1446
类似的问题也在讨论中,但我相信我的数据是复杂的,我不能做更多。我所能做的就是编写一部分代码,一直读到“dd-mm-yyyy”:
clear;clc;close all;
f = fopen('dataa_file.txt');
line = fgetl(f);
while isempty(strfind(line, 'nT_GSE'))
if line == -1 %// If we reach the end of the file, get out
break;
end
line = fgetl(f);
end
任何帮助都将不胜感激……这似乎有效。它假定
- 包含数字的第一行紧跟在以
开头的行之后'dd-mm-yyyy'
- 包含数字的最后一行是以
开头的行上方的两行“#键参数”
谢谢,成功了。如果你能详细说明所使用的各种命令,那将是非常有益的。。
t = fileread('file.txt'); % Read the file as a character vector
t = strsplit(t, {'\r' '\n'}, 'CollapseDelimiters', true); % Split on newline or carriage
% return. This gives a cell array with each line in a cell
ind_start = find(cellfun(@any, regexp(t, '^dd-mm-yyyy', 'once')), 1) + 1; % index of
% line where the numbers begin: immediately after the line 'dd-mm-yyyy...'
ind_end = find(cellfun(@any, regexp(t, '^# Key Parameter', 'once')), 1) - 2; % index of
% line where numbers end: two lines before the line '# Key Parameter...'
result = cellfun(@(u) sscanf(u, '%d-%d-%d %02d:%02d:%02d.%d.%d.%d.%d %f %f %f %f').', ...
t(ind_start:ind_end), 'UniformOutput', false);
% Apply sscanf to each line. The format specifier uses %d where needed to prevent
% the dot from being interpreted as part of a floating point number. Also, the
% possible existence of leading zeros needs to be taken into account. The result is
% a cell array, where each cell contains a numeric vector corresponding to one line
result = cell2mat(result.'); % convert the result to a numerical array