Extracting single lines of text from a .txt file

5 views (last 30 days)
Greetings MATLAB community,
Let me start by saying I am completely new to coding, and therefore am nascent to the various functions MATLAB offers.
I am trying to get a single line from a .txt file and import it as a variable on MATLAB. This line is simply 'WaitingforScanner.RTTime:' and then there will be a number (a time stamp in milliseconds). I am trying to do this for one file in particular right now, but will need to do this for 334 files soon after. How would I go about importing this one line from the .txt file into my MATLAB workspace?
Thank you for your help,
Will
  1 Comment
dpb
dpb on 7 Apr 2021
In general, unless the file(s) are very large, it's more efficient to just read the whole file and then locate the wanted data in the data in memory. readlines would be a good candidate for this followed up with contains and extractAfter and str2double and maybe duration

Sign in to comment.

Answers (1)

dpb
dpb on 7 Apr 2021
In general, unless the file(s) are very large, it's more efficient to just read the whole file and then locate the wanted data in the data in memory. readlines would be a good candidate for this followed up with contains and extractAfter and str2double and maybe duration
Alternatively, if the files are of a fixed and known format so the desired data is always the same record in the file, one could use textscan and a format string with the 'NumHeaderLines' optional argument to skip the unwanted lines prior to the desired record.
If it is not always in the same place, then the alternative is to read each record in turn and parse each until find the magic string.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!