Read text file, identify variables, combine some the variables and write these variables to another text file
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi. have a text file with contents as shown below
99 4.00391406E-01 Aqu: P, X_m_A, X_i_A, T 0.11600000E-14 0.11600000E-14 0.11600000E-14 0.40000000E+00
1.2850440226589E+07 6.6703655432914E-15 3.0500000000000E-02 1.4644900812579E+01
1 0 4.00391406E-01 Aqu: P, X_m_A, X_i_A, T 0.11600000E-14 0.11600000E-14 0.11600000E-14 0.40000000E+00
1.2850440226474E+07 6.6703644497117E-15 3.0500000000000E-02 1.4644900812901E+01
1 1 4.00391406E-01 Aqu: P, X_m_A, X_i_A, T 0.11600000E-14 0.11600000E-14 0.11600000E-14 0.40000000E+00
1.2850440225837E+07 6.6703545162559E-15 3.0500000000000E-02 1.4644900815972E+01
1 9 4.00391406E-01 Aqu: P, X_m_A, X_i_A, T 0.11600000E-14 0.11600000E-14 0.11600000E-14 0.40000000E+00
1.2850440225793E+07 6.6703531321840E-15 3.0500000000000E-02 1.4644900816457E+01
199 4.01221065E-01 Aqu: P, X_m_A, X_i_A, T 0.11600000E-14 0.11600000E-14 0.11600000E-14 0.40000000E+00
1.2892950904933E+07 1.3658209434909E-11 3.0500000000025E-02 1.4079185013275E+01
2 0 4.01221065E-01 Aqu: P, X_m_A, X_i_A, T 0.11600000E-14 0.11600000E-14 0.11600000E-14 0.40000000E+00
1.2892950907392E+07 1.3639821383553E-11 3.0500000000025E-02 1.4079185012785E+01
2 1 4.01221065E-01 Aqu: P, X_m_A, X_i_A, T 0.11600000E-14 0.11600000E-14 0.11600000E-14 0.40000000E+00
1.2892950911721E+07 1.3662519315952E-11 3.0500000000025E-02 1.4079185009451E+01
2 2 4.01221065E-01 Aqu: P, X_m_A, X_i_A, T 0.11600000E-14 0.11600000E-14 0.11600000E-14 0.40000000E+00
1.2892950916475E+07 1.3653019041868E-11 3.0500000000025E-02 1.4079185010697E+01
9999 5.99941351E-01 AqH: P, S_aqu, X_i_A, T 0.10000000E-13 0.10000000E-13 0.10000000E-13 0.60000000E+00
1.2064314164598E+07 2.9992851939779E-01 3.0500067234545E-02 8.5203189341152E+00
100 0 5.99957219E-01 AqH: P, S_aqu, X_i_A, T 0.10000000E-13 0.10000000E-13 0.10000000E-13 0.60000000E+00
1.2066959033632E+07 2.9994785087822E-01 3.0500049961530E-02 8.5208367894259E+00
100 1 5.99975657E-01 AqH: P, S_aqu, X_i_A, T 0.10000000E-13 0.10000000E-13 0.10000000E-13 0.60000000E+00
1.2070032134807E+07 2.9997031185712E-01 3.0500029877690E-02 8.5214386345461E+00

I am trying to do two things here:
1.- Join the element names by add the mising '0' in the element name.
2. Store all these variables, whh has already been answered in the thread link here: https://in.mathworks.com/matlabcentral/answers/1866703-read-text-file-identify-variables-and-rewrite-some-of-the-variables?s_tid=mlc_lp_leaf
0 comentarios
Respuestas (1)
Tridib
el 13 de Jun. de 2025
Editada: Tridib
el 13 de Jun. de 2025
To achieve the desired result, “regexp” and “regexprep” can be used. In the code referenced earlier, after reading the file into a character vector and before applying the reshape function:
1. Use “regexp” to detect lines where the first element name contains a space (the pattern looks for newline or start-of-file, followed by digits, a space, then more digits).
2. Apply “regexp” with 'tokens' to extract each matched digit pair into a cell array.
3. For each pair, construct the original pattern and its replacement string.
4. Replace the first occurrence of each pattern in the data string using “regexprep” to ensure only valid, non-repeating substitutions.
pattern = '\n(\d+)\s+(\d+)';
matches = regexp(data, pattern, 'tokens');
for k = 1:length(matches)
original = ['\n' matches{k}{1} ' ' matches{k}{2}];
replacement = ['\n' matches{k}{1} '0' matches{k}{2}];
data = regexprep(data, original, replacement, 1); % only first occurrence each time
end
For more help, refer to the following documentations:
Hope this helps!
0 comentarios
Ver también
Categorías
Más información sobre Text Data Preparation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!