Find data from txt file

Hi all,
I try to find a data from txt file. In a row (i don't known which row), there is an information like "Dt: 0.0001". I want to find this row and store 0.0001 as a variable. How can i do that?
Thanks for help

5 comentarios

Akira Agata
Akira Agata el 29 de Abr. de 2024
How about using startsWith (to detect "DT:") + split (to split "DT" and data) + str2double (to covert to double)?
KSSV
KSSV el 30 de Abr. de 2024
Attach your text file.
Cem Eren Aslan
Cem Eren Aslan el 30 de Abr. de 2024
There are two types of txt file. I attach both of them below:
First txt type:
PEER STRONG MOTION DATABASE RECORD. PROCESSING BY PACIFIC ENGINEERING.
IMPERIAL VALLEY 5/19/40 0439, EL CENTRO ARRAY #9, 180 (USGS STATION 117)
ACCELERATION TIME HISTORY. FILTER POINTS: HP=0.2 Hz LP=15.0 Hz
NPTS= 4000, DT= .01000 SEC
Acceleration (m/sec2)
-0.06281522
-0.05914169
0.00520338
0.07596138
0.06759460
....
Second txt type:
Earthquake: Imperial Valley 10/15/79
Station: El Centro Array #11
Component: 230
Dt: 0.005 sec
# of Data: 7807
Source: PEER Strong Motion Database
Site Class: Z3
Acceleration (m/sec2)
0.0267573
0.0294902
0.011642
-0.0165857
-0.0165209
-0.0169276
.....
Stephen23
Stephen23 el 30 de Abr. de 2024
@Cem Eren Aslan: please upload a sample data file by clicking the paperclip button.

Iniciar sesión para comentar.

Respuestas (2)

Voss
Voss el 30 de Abr. de 2024
Editada: Voss el 30 de Abr. de 2024
filename = 'file1.txt';
dt = str2double(regexpi(fileread(filename),'dt. (.*) sec','tokens','once'))
dt = 0.0100
filename = 'file2.txt';
dt = str2double(regexpi(fileread(filename),'dt. (.*) sec','tokens','once'))
dt = 0.0050
Mathieu NOE
Mathieu NOE el 30 de Abr. de 2024
Simply using the suggested methods , you can access your DT data this way
I simply created two data files from you post
I used lower to convert all characters to lower case which then ease the process
% first file
out = fileread('data1.txt')
out =
'PEER STRONG MOTION DATABASE RECORD. PROCESSING BY PACIFIC ENGINEERING. IMPERIAL VALLEY 5/19/40 0439, EL CENTRO ARRAY #9, 180 (USGS STATION 117) ACCELERATION TIME HISTORY. FILTER POINTS: HP=0.2 Hz LP=15.0 Hz NPTS= 4000, DT= .01000 SEC Acceleration (m/sec2) -0.06281522 -0.05914169 0.00520338 0.07596138 0.06759460'
str = extractBetween(lower(out),'dt','sec');
A = regexp(str,'[-+]?([0-9]*[.])?[0-9]+([eE][-+]?\d+)?','match'); % extract numerical content of string
dt_value = str2double(A{1})
dt_value = 0.0100
% second file
out = fileread('data2.txt')
out =
'Earthquake: Imperial Valley 10/15/79 Station: El Centro Array #11 Component: 230 Dt: 0.005 sec # of Data: 7807 Source: PEER Strong Motion Database Site Class: Z3 Acceleration (m/sec2) 0.0267573 0.0294902 0.011642 -0.0165857 -0.0165209'
str = extractBetween(lower(out),'dt','sec');
A = regexp(str,'[-+]?([0-9]*[.])?[0-9]+([eE][-+]?\d+)?','match'); % extract numerical content of string
dt_value = str2double(A{1})
dt_value = 0.0050

3 comentarios

Mathieu NOE
Mathieu NOE el 28 de Mayo de 2024
hello again
problem solved ?
Cem Eren Aslan
Cem Eren Aslan el 29 de Mayo de 2024
Yes but not this way. I solved the problem by using AI. Thank you
Voss
Voss el 29 de Mayo de 2024
@Cem Eren Aslan: What solution did AI give you?

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 29 de Abr. de 2024

Comentada:

el 29 de Mayo de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by