Effacer les filtres
Effacer les filtres

Create numerical table from text file

2 vues (au cours des 30 derniers jours)
Marisabel Gonzalez
Marisabel Gonzalez le 17 Déc 2018
Commenté : TADA le 17 Déc 2018
Here I attached one file from which I want to extract some values and place them on a table. Example, let's say I want the Elsasser Number, the Power and the Lorentz number. How would I look for those paricular keywords and extract the values and place them on a table with headlines?
I have several files similar to this one so it would be really painful to manually set up the table. Therefore I would need a loop...

Réponse acceptée

TADA
TADA le 17 Déc 2018
text = fileread('example.txt');
% extract data
match = regexpi(text, '(?<key>(Elsasser\s*Number)|(Lorentz\s*Number)|(Power\s*(\([^\)]+\))?))\s*=\s*(?<val>(\s*\d+(\.\d+)?)+)', 'names')
match =
1×3 struct array with fields:
key
val
% display struct content to command window
struct2table(match, 'AsArray', true)
ans =
3×2 table
key val
______________________ __________________________
'Elsasser Number' '0.309188 0.309188'
'Lorentz Number' '0.001758'
'Power (eq 21 of C&A)' '20299.521762'
I used a regular expression to extract the data into a struct array containing a key-value pair
It's worth mentioning that this regexp works for the supplied file, but may break for other examples
regular expressions are very useful for such tasks, and to better understand, I suggest you search in google for a regular expression builder. most of them are good enough. some have very good teaching modules which help you learn how to write expressions
  2 commentaires
Marisabel Gonzalez
Marisabel Gonzalez le 17 Déc 2018
(Power\s*(\([^\)]+\))?))\s*=\s*(?<val>(\s*\d+(\.\d+)?)+)
uau.
Thank you for your help, this would be a great starting point!
TADA
TADA le 17 Déc 2018
cheers! good luck

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by