getting a specific paragraph from a .txt file

1 回表示 (過去 30 日間)
Jamie Shelley
Jamie Shelley 2016 年 7 月 14 日
コメント済み: Jamie Shelley 2016 年 7 月 16 日
This might sound like a stupid question, but is there was way of retrieving a specific paragraph from a .txt file please? I've written the conditions for various experiments in a txt file, so like
test1
-
-
test2
-
-
and so on. I was wondering if I could say get the user to input the test number (on a gui) and then use a strcat function to merge test and the number and then somehow retrieve the paragraph that corresponds to that number so I can then display it on the gui. Is there a way of doing that please?
Thanks

採用された回答

Walter Roberson
Walter Roberson 2016 年 7 月 15 日
filecontent = fileread('YourFile.txt');
testnum = 2;
par_begin_pos = regexp(filecontent, sprintf('^test%d', testnum), 'lineanchors');
if isempty(par_begin_pos)
fprintf('No paragraph #%d in file', testnum);
return
end
par_end_pos = regexp(filecontent, sprintf('^test%d', testnum+1), 'lineanchors');
if isempty(par_end_pos)
par_end_pos = length(filecontent) + 1; %right test was last thing in file
end
paragraph_content = filecontent(par_begin_pos : par_end_pos - 1);
  1 件のコメント
Jamie Shelley
Jamie Shelley 2016 年 7 月 16 日
Thank you!

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 14 日
You can use regexp
  2 件のコメント
Jamie Shelley
Jamie Shelley 2016 年 7 月 14 日
Thanks but what is that please?
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 14 日

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

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by