代码不会在MATLAB中运行

时间:2013-12-06 04:41:54

标签: matlab while-loop

我有一个while循环,它会执行但会无限期地运行。我不确定会导致这种情况发生的原因。任何帮助将不胜感激。

Fid=fopen('plaintext.txt');
str= fgetl(Fid);
count=0;, count1=0;, count2= 0;, count3=0;

while ischar(str);
if regexp(str, '\w+');
    count= count + 1;
end
count1=count1+length(str);
if regexp(str, 'I');
    count2= count2 + 1;
end
if regexp(str, 'the');
    count3= count3 + 1;
end
if regexp(str, '#');
    count4= count4 + 1;
end
end

1 个答案:

答案 0 :(得分:0)

fgetl功能移至while循环:

Fid=fopen('plaintext.txt');

count=0;, count1=0;, count2= 0;, count3=0; count4=0;
while 1
   str= fgetl(Fid);
   if ~ischar(str);
      break;
   end
   if regexp(str, '\w+');
      count= count + 1;
   end
   count1=count1+length(str);
   if regexp(str, 'I');
      count2= count2 + 1;
   end
   if regexp(str, 'the');
      count3= count3 + 1;
   end
   if regexp(str, '#');
      count4= count4 + 1;
   end
end