Extracting single lines of text from a .txt file
조회 수: 5 (최근 30일)
이전 댓글 표시
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
2021년 4월 7일
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
2021년 4월 7일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!