How can I extract a single value from every 86 lines of a long text file?

1 visualización (últimos 30 días)
The file repeats every 86 lines. I am trying to extract every 86th row, starting with row 1. I am attaching a small section of the text file below, and my attempt below that. The full file is 39904 lines long with a total of 39904/86=464 numbers to extract.
58.53 185.05 179.93 0.00 192.17 192.17
58.58 127.17 113.18 0.00 128.08 128.08
58.64 121.15 107.12 0.00 121.93 121.93
58.69 117.62 103.56 0.00 118.29 118.29
58.75 115.12 101.02 0.00 115.68 115.68
58.80 113.18 99.05 0.00 113.64 113.64
58.86 111.59 97.43 0.00 111.96 111.96
58.91 110.25 96.06 0.00 110.53 110.53
58.96 109.09 94.86 0.00 109.28 109.28
59.02 108.06 93.81 0.00 108.17 108.17
59.51 102.03 87.58 0.00 101.57 101.57
SUMMED 1/3 OCTAVE BAND SPECTRA
OBSERVER 2 LOCATED AT X= 40731.000 Y= 0.0000000 Z= 17996.158
FREQUENCIES 0.50000E+02 0.63000E+02 0.80000E+02 0.10000E+03 0.12500E+03 0.16000E+03 0.20000E+03 0.25000E+03
0.31500E+03 0.40000E+03 0.50000E+03 0.63000E+03 0.80000E+03 0.10000E+04 0.12500E+04 0.16000E+04
0.20000E+04 0.25000E+04 0.31500E+04 0.40000E+04 0.50000E+04 0.63000E+04 0.80000E+04 0.10000E+05
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.53 SPL 150.67 152.00 153.15 154.07 154.94 155.97 156.81 157.54
SPL 158.13 158.37 158.20 157.64 156.80 155.83 154.54 152.98
SPL 151.85 150.14 148.67 147.31 145.89 144.71 144.27 146.16
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.58 SPL 118.48 119.38 119.91 119.37 118.28 116.32 114.21 112.08
SPL 109.85 107.47 105.31 103.15 101.01 99.13 98.28 95.48
SPL 93.74 92.44 89.93 88.09 85.99 83.98 82.30 81.38
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.64 SPL 112.47 113.37 113.90 113.36 112.27 110.31 108.20 106.07
SPL 103.83 101.45 99.28 97.10 94.94 93.03 92.13 89.26
SPL 87.43 86.01 83.33 81.26 78.90 76.48 74.36 73.07
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.69 SPL 108.95 109.85 110.38 109.84 108.75 106.79 104.68 102.55
SPL 100.30 97.91 95.73 93.54 91.36 89.42 88.48 85.54
SPL 83.62 82.07 79.23 76.92 74.29 71.47 68.92 67.26
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.75 SPL 106.45 107.35 107.88 107.34 106.25 104.29 102.18 100.04
SPL 97.80 95.40 93.21 91.01 88.80 86.83 85.84 82.84
SPL 80.83 79.16 76.15 73.61 70.71 67.49 64.49 62.47
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.80 SPL 104.51 105.42 105.94 105.40 104.31 102.35 100.24 98.10
SPL 95.85 93.45 91.25 89.03 86.80 84.80 83.77 80.69
SPL 78.59 76.81 73.62 70.85 67.69 64.08 60.63 58.24
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.86 SPL 102.93 103.83 104.36 103.82 102.72 100.76 98.65 96.51
SPL 94.26 91.85 89.65 87.41 85.16 83.13 82.05 78.90
SPL 76.71 74.81 71.46 68.45 65.03 61.02 57.13 54.37
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.91 SPL 101.59 102.49 103.02 102.48 101.38 99.42 97.31 95.17
SPL 92.91 90.50 88.28 86.03 83.76 81.69 80.58 77.36
SPL 75.08 73.05 69.53 66.30 62.61 58.21 53.88 50.75
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
58.96 SPL 100.43 101.33 101.86 101.32 100.22 98.26 96.14 94.00
SPL 91.74 89.32 87.10 84.84 82.54 80.44 79.28 75.99
SPL 73.62 71.48 67.79 64.32 60.37 55.58 50.81 47.31
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
59.02 SPL 99.40 100.31 100.83 100.29 99.20 97.23 95.12 92.97
SPL 90.71 88.28 86.05 83.78 81.46 79.33 78.12 74.77
SPL 72.30 70.04 66.19 62.49 58.28 53.10 47.87 44.01
OBS. TIME SOUND PRESSURE LEVEL (SPL) FOR EACH FREQUENCY
59.51 SPL 93.40 94.30 94.82 94.28 93.18 91.21 89.08 86.92
SPL 84.63 82.15 79.84 77.43 74.91 72.50 70.91 66.93
SPL 63.67 60.34 55.00 49.26 42.73 34.12 24.91 17.70
TIME HISTORIES OF THE NOISE LEVELS AT THE OBSERVER POSITIONS
( 0.00 MEANS VALUE WAS NOT COMPUTED )
OBSERVER 2 ( 40731.000 0.0000000 17996.158 )
OBSERVER OVERALL A-WEIGHTED D-WEIGHTED PERCEIVED NOISE TONE-CORRECTED
TIME SPL SPL SPL LEVEL (PNL) PNL (PNLT)
(DB) (DB(A)) (DB(D)) (PNDB) (PNDB)
------------------------------------------------------------------------------------------------------------------------------
58.53 168.41 165.04 0.00 177.10 177.10
My attempt:
close all; clc; clear all;
fid = fopen("fort18000.txt",'r');
for j = 1:464
data(j,:) = textscan(fid,'%f %f %f %f %f %f /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n /n');
end
My understanding is that the 86 /n specifiers should skip 86 lines after line 1, then the loop will repeat. However, my results from this provide all of the first 11 rows from the text file and nothing else. I've been stumped by this for a while. Any advice?
  2 comentarios
per isakson
per isakson el 23 de Abr. de 2020
Editada: per isakson el 23 de Abr. de 2020
The content that you have included in your question contains 87 lines
Is it this line and corresponding lines in the following blocks that you want to retrieve?
58.53 185.05 179.93 0.00 192.17 192.17
Stephen Willoughby
Stephen Willoughby el 23 de Abr. de 2020
Editada: Stephen Willoughby el 23 de Abr. de 2020
Thanks for the comment. I want every 86th line starting at line 1. So thats line 1, 87, 173 (not shown above), ..., 39904. So ultimately what I'm looking for is:
58.53 185.05 179.93 0.00 192.17 192.17
58.53 168.41 165.04 0.00 177.10 177.10
.
.
.

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 23 de Abr. de 2020
Editada: per isakson el 24 de Abr. de 2020
Try this
%% Read all lines as text
fid = fopen( 'fort18000.txt' );
cac = textscan( fid, '%s', 'Delimiter','\n' );
[~] = fclose( fid );
cac = cac{1};
%% Parse every N:th line
N = 86;
len = length(cac);
out = nan( len/N, 6 );
kk = 0;
for jj = 1 : N : len
buf = textscan( cac{jj}, '%f%f%f%f%f%f' );
kk = kk +1;
out(kk,:) = [buf{:}];
end

Más respuestas (0)

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by