我正在尝试导出一组数据,对此我非常了解。有问题的数据具有以下结构:
# ************************************
# ***** 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…。”和“##关键参数”之间的内容导出到列中。 例如。 ,第一行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,223,693,846,740,1.43208E + 09,1.14132,9.14226和27.1446。
类似的问题已在Use MATLAB to extract data beyond "Data starts on next line:" in text-file中解决,但我认为我的数据很复杂,无法做进一步的事情。我能做的最好的事情就是编写一部分代码,直到“ 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
任何帮助将深表感谢……
答案 0 :(得分:2)
这似乎有效。假设
'dd-mm-yyyy'
开头的行之后的数字。'# Key Parameter'
开头的行的上方两行。代码:
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