sreeraj t
sreeraj t

Reputation: 2369

Using matlab to extract data in between text

I am trying to export a set of data and I am pretty knew to this. The data in question has this structure:

#              ************************************
#              *****    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

I require those written between “dd-mm-yyyy….” and “# # Key Parameter” to be exported to columns. E.g. , the first line 21-05-2015 00:00:00.223.693.846.740 1.43208E+09 1.14132 9.14226 27.1446, has to exported into 21, 05,2015, 00,00,00,223,693,846,740, 1.43208E+09,1.14132, 9.14226 and 27.1446.

Similar question is tackled at Use MATLAB to extract data beyond "Data starts on next line:" in text-file but I believe my data is complicated and I could not do further. The best I could do was to write a part of code to read till “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

Any help will be deeply appreciated…

Upvotes: 1

Views: 171

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112679

This seems to work. It assumes that

  • The first line that contains numbers is that immediately after the line that begins with 'dd-mm-yyyy'.
  • The last line that contains numbers is two lines above the line that begins with '# Key Parameter'.

Code:

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

Upvotes: 2

Related Questions