Pulling out a number and a date + time from text file after specific lines
1 view (last 30 days)
Show older comments
Heidi Mäkitalo
on 27 Jun 2019
Edited: Heidi Mäkitalo
on 2 Jul 2019
Hello all,
I have the following two lines in my text file:
Time of Interval 0: 03/03/18 18:14:02
Interval: 0.02 sec
First off, how can I pull out the number after "Interval:" ?
Secondly, how can I do the same for the date and time after "Time of Interval 0:" ?
The number and the date vary from file to file, but the text preceding them is always the same. I need a solution that works in every situation.
Thanks in advance!
0 Comments
Accepted Answer
Bob Thompson
on 27 Jun 2019
The most reliable way that I know of to get this is by using fgetl.
file = fopen('mytextfile.txt'); % Access the file
line = fgetl(file); % Read first line
c = 1; % Record the line number for debugging purposes
while ~isnumeric(line) & ~strcmp(line(1:16),'Time of Interval') % Check for desired line, or end of file
line = fgetl(file); % Read next line
c = c + 1; % Advance line count
end
parts = strsplit(line,': ');
number = str2num(parts{1}(18:end));
dt = datetime(parts{2});
There may be some thing syou need to tweak to make it personalized to your inputs, but that should work.
1 Comment
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!