Azzera filtri
Azzera filtri

How to extract specific text (eg. serial number) from single/multiple text file

2 visualizzazioni (ultimi 30 giorni)
Hi guys,
I am currently having a log file where i required to extract the serial number and some information from the text log file. The serial number are not always in the same location of the text file, it may be at beginning or next few line of the text file. Is there anywhere i can extract the information like search/find function?

Risposte (1)

Bob Thompson
Bob Thompson il 2 Apr 2019
It is possible to use find or strcmp, but I think your best bet for this is to use regexp. If the serial number is in a consistent and unique format then you can simply search for a series of characters which match that format and extract it. It may not be the quickest, but it should work.
text = textscan('mytxtfile.txt','%s');
sn = regexp(text,'words* (\d\d\d\d\d\d\d\d\d\d\d) more words*','tokens'); % Should extract an 11 digit number buried in some words, adjust as needed

Community Treasure Hunt

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

Start Hunting!

Translated by