MATLAB Answers

How to read this kind of info out of a txt file and post them into Excel files?

1 view (last 30 days)
Leon
Leon on 7 May 2021
Edited: dpb on 7 May 2021
I receive the below info from emails and would store them into an txt file named FmEmail.txt. The info is composed of several groups of unknown amount of terms. Sometimes just one group, sometimes two groups, other times could have all 3 groups.
Instruments:
instrument 1
instrument 2
Institutions:
institutions 1
institutions 2
institutions 3
Variables
Var 1
Var 2
Var 3
Var 4
Var 5
How do I extract each group of info out and dump them into the first column of their own respective Excel files? For example, the variables would go to an Excel file named Variable.xlsx, which stores a 3 column matrix like the below:
Varnames, Var_ID, Description
Var 11 nnn XYZ
...
etc.
Many thanks.

Accepted Answer

dpb
dpb on 7 May 2021
Edited: dpb on 7 May 2021
Basically, just read the file record-by-record searching for the matching header strings and when find a section header, then read that section type data until run out of those. At that point, check for which section header string it is that you just found to handle those.
Issues are if sections are all there, but aren't necessarily in same order or if the file can/does contain multiple email messages that encompass more than one dataset so you can have the same header multiple times and in different orders. Not enough info provided to know just how complicated your search logic must be.
If the files aren't too big, probably the easiest way will be to suck the whole thing up into a cellstr() or string() array and use ismember and/or contains to find the header positions globally first and then iterate through those indices to handle each section in turn.
Obviously the sample file also isn't complete enough to have any information about the actual data so how to parse it iwhen do locate it is yet to be resolved as well...
More questions yet than answers, but the general idea is "just search for the needle in the haystack" Sometimes it's a silver needle, sometimes gold.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by