Использование Matlab для извлечения данных между текстом - PullRequest
1 голос
/ 30 мая 2019

Я пытаюсь экспортировать набор данных, и я довольно об этом знаю. Данные имеют следующую структуру:

#              ************************************
#              *****    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, должна быть экспортирована в 21, 05,2015, 00,00,00,223,693,846,740, 1,43208E + 09,1.14132, 9.14226 и 27,1446.

Аналогичный вопрос решается на Используйте MATLAB для извлечения данных за пределы «Данные начинаются со следующей строки:» в текстовом файле , но я считаю, что мои данные сложны, и я не мог сделать дальше. Лучшее, что я мог сделать, это написать часть кода для чтения до «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

Любая помощь будет высоко оценена ...

1 Ответ

2 голосов
/ 30 мая 2019

Это похоже на работу. Предполагается, что

  • Первая строка, содержащая числа, состоит в том, что сразу после строки, начинающейся с '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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...