How could I not including the heading of the chapter?

1 次查看(过去 30 天)
I wrote this function to extract the texts of one chapter out of the book - Alice in Wonderland.
When I run it, it gives me " '*CHAPTER 1*--DOWN THE RABBIT-HOLEAlice was beginning to get very tired of sitting by her sister on thebank,...."
However, I want only the texts, not including the title of the chapter. How could I fix it?
function key = getChapter(keyFile, chapter)
% key is a char row vector containing all the text in the specified chapter
% of keyFile.
% chapter: the number of the chapter to be used as the key, an integer
fid=fopen(keyFile,'r');
chap1=['*CHAPTER ',num2str(chapter),'*'];
chap2=['*CHAPTER ',num2str(chapter+1),'*'];
key='';
inchapter=0;
while ~feof(fid)
s=fgetl(fid);
if length(s)>=length(chap2) && strcmp(s(1:length(chap2)),chap2)==1 %find he "chapter 1"setence, real text should start from below
inchapter=0;
elseif length(s)>=length(chap1) && strcmp(s(1:length(chap1)),chap1)==1 %find the "chapter 2" sentence, real text should end b4 it
inchapter=1;
end
if length(s)>length(chap1)&& inchapter==1
key=[key,s];
end
end
fclose(fid);
  1 个评论
Athul Prakash
Athul Prakash 2020-11-19
You could trim the character vector 's' to remove the chapter headings -
s(1:length(chap1))='' % or use 'chap2' as appropriate

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by