MATLAB Answers

Extracting single lines of text from a .txt file

조회 수: 13(최근 30일)
William Denomme
William Denomme 7 Apr 2021
답변: dpb 7 Apr 2021
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
dpb
dpb 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

댓글을 달려면 로그인하십시오.

답변(1개)

dpb
dpb 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.

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by