How do I identify the a specific row number in a .txt file?

16 views (last 30 days)
Hello folks,
Ive got a .txt file shown above. I want to identify the row number of the circled entry (Time (s)). So, if im not mistaken, 26.
I read other similair questions and tried this:
fid = fopen('C:\wherever\textymctextface.txt', 'rt');
s = textscan(fid, '%s', 'delimiter', '\n');
loc = find(strcmp(s{1}, 'Time (s)'), 1, 'first');
So the struct s created looks fine, until the 17th row (Type Time Duration Comment). In this row, it groups the next 6 rows in that same row. So in Matlab, the variable looks like:
What gives? Why does this happen? The new row delimeter is the same throughout, and since the number of Comments is different for different files I cant just account for them.
So: how do I fix my code or does anyone have any other ideas to go about this?
Thank you!!!
R

Accepted Answer

Rafael Cordero
Rafael Cordero on 4 Aug 2017
Ok solved it in a not very elegant way. I dont know why the /n wasnt being reconised in textscan but what I did was call text scan AGAIN on just that problem row and then it worked fine. I then added the row numbers and added the offset of blank rows:
fid = fopen('C:\Users\Rafa\Documents\Data\SonRMay172017Experiment\H17-1001-01_004_Subcutaneous SonR.txt', 'rt');
s = textscan(fid, '%s', 'delimiter', '\n');
ss=textscan(s{1,1}{17,1}, '%s', 'delimiter', '\n');
channel_title_row=17+length(ss{1,1})+1;
fclose(fid);

More Answers (1)

fbaillon
fbaillon on 4 Aug 2017
Have you tried with the delimiters '\r\n'?
s = textscan(fid, '%s', 'delimiter', '\r\n');

Community Treasure Hunt

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

Start Hunting!