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

8 ビュー (過去 30 日間)
Rafael Cordero
Rafael Cordero 2017 年 8 月 4 日
回答済み: Rafael Cordero 2017 年 8 月 4 日
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

採用された回答

Rafael Cordero
Rafael Cordero 2017 年 8 月 4 日
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);

その他の回答 (1 件)

fbaillon
fbaillon 2017 年 8 月 4 日
Have you tried with the delimiters '\r\n'?
s = textscan(fid, '%s', 'delimiter', '\r\n');
  1 件のコメント
Rafael Cordero
Rafael Cordero 2017 年 8 月 4 日
Gives me an empty empty 1x1 cell array.
What does the '\r\n' do?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by