How could I not including the heading of the chapter?

1 view (last 30 days)
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 Comment
Athul Prakash
Athul Prakash on 19 Nov 2020
You could trim the character vector 's' to remove the chapter headings -
s(1:length(chap1))='' % or use 'chap2' as appropriate

Sign in to comment.

Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!