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
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 Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!