Read data from .txt with regexp
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aurea94
el 29 de Mzo. de 2022
Comentada: Rik
el 18 de Abr. de 2022
I want to load numeric data from a .txt file, which contains both string a numeric and contains non standard arranging. I am attaching an example file for you to see.
The data is divided in sections, but all the numeric data I am interested in have the same structure: '->' + 'some string' + ':' + 'numeric value with format %.4f' (Lines in the .txt not containing the desired expression should not be considered).
I tested different manners of doing so, with readfile, testscan etc., however due to the complexitu of the input file I could not reach my objective.
I think the most appropiate way to try to solve the problem is by using regexp and properly telling matlab which is the expression it has to look for in the .txt file.
I am not familarised with regexp and cannot properly understand how to code the proper expression for me to work, and would greatly appreaicate any help with that.
Thank you very much!
============================================================================
| DATA VALUES FOR INPUT VARIABLES |
============================================================================
Model: Example
(data file produced by on 29-Mar-2022 17:30:14)
============================================================================
FIRST DATA SECTION:
============================================================================
-> z [val]: 13.0000
-> m [in mm]: 3.0000
-> Type [str]: Ball
-> c_s [valval]: 0 0 0
----------------------------------------------------------------------------
SECOND DATA SECTION:
============================================================================
-> r_b [strandnumber]: C50
-> s0_r [in mm]: 0.0000
-> I_r [val]: 15.0000
-> Only text line
----------------------------------------------------------------------------
THIRD DATA SECTION:
============================================================================
-> Values found = 0.
----------------------------------------------------------------------------
FOURTH DATA SECTION:
============================================================================
-> n_1 [val_1]: 1.0000
5 comentarios
Rik
el 30 de Mzo. de 2022
For basic data like this, even JSON should be preferred. Just about every half-decent programming language can read it. Composing matrices is a bit tricky, but as long as you stick to scalars or vectors everything should be fine.
Respuesta aceptada
Stephen23
el 29 de Mzo. de 2022
Editada: Stephen23
el 29 de Mzo. de 2022
rgx = '^\s*->\s*(\w+)\s*\[[\w\s]+\]:\s*([^\r\n]+)';
str = fileread('ExampleTextFile.txt');
tkn = regexp(str,rgx,'tokens','lineanchors');
tkn = vertcat(tkn{:})
vec = str2double(tkn(:,2));
idx = ~isnan(vec);
tkn(idx,2) = num2cell(vec(idx))
out = cell2struct(tkn(:,2),tkn(:,1),1)
4 comentarios
Md Kamrul Hasan Ikbal
el 18 de Abr. de 2022
@Stephen; Can I get your email. I need a favour from you.
Rik
el 18 de Abr. de 2022
I expect your chances would be much better if you posted your request as a separate question and post the link here. People tend to be protective of their inbox.
Más respuestas (0)
Ver también
Categorías
Más información sobre String Parsing 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!