Matlab提供了兩個(gè)函數(shù)fgetl和fgets來從格式文本文件讀取行,并存儲(chǔ)到字符串向量中。這兩個(gè)函數(shù)集幾乎相同;不同之處是,fgets拷貝新行字符到字符向量,而fgetl則不。
下面的M-file函數(shù)說明了fgetl的一個(gè)可能用法。此函數(shù)使用fgetl一次讀取一整行。對(duì)每一行,函數(shù)判斷此行是否包含給定的文字。
CODE:
function y = litcount(filename, literal)
% Search for number of string matches per line.
fid = fopen(filename, 'rt');
y = 0;
while feof(fid) == 0
tline = fgetl(fid);
matches = findstr(tline, literal);
num = length(matches);
if num > 0
y = y + num;
fprintf(1,'%d:%s\n',num,tline);
end
end
fclose(fid);
badpoem文件如下
Oranges and lemons,
Pineapples and tea.
Orangutans and monkeys,
Dragonflys or fleas.
運(yùn)行實(shí)例:
litcount('badpoem','an')
2: Oranges and lemons,
1: Pineapples and tea.
3: Orangutans and monkeys,
參考文獻(xiàn):
matlab7.0幫助文檔
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。