检查if else语句和循环文件

时间:2014-04-30 12:23:24

标签: matlab matlab-figure

我想循环浏览文件并检查每个文件是否包含任何拼写错误

 If yes then return 1 else -1.

现在,我可以检查1个文件是否存在拼写错误。但它不能循环超过1并且如果得到错误则返回1,否则为-1。

checkSpelling.m文件

function suggestion = checkSpelling(word)

h = actxserver('word.application');
h.Document.Add;
correct = h.CheckSpelling(word);
if correct
  suggestion = []; %return empty if spelled correctly
else
  %If incorrect and there are suggestions, return them in a cell array
  if h.GetSpellingSuggestions(word).count > 0
      count = h.GetSpellingSuggestions(word).count;
      for i = 1:count
          suggestion{i} = h.GetSpellingSuggestions(word).Item(i).get('name');
      end
  else
      %If incorrect but there are no suggestions, return this:
      suggestion = 'no suggestions';
  end

end
%Quit Word to release the server
h.Quit

f20.m文件

for i = 1:10

data2=fopen(strcat('DATA\',int2str(i),''),'r')
CharData = fread(data2, '*char')';  %read text file and store data in CharData
fclose(data2);

 word = regexp(CharData, ' ', 'split')

[sizeData b] = size(word)

suggestion = cellfun(@checkSpelling, word, 'UniformOutput', 0)
if sum(cellfun(@isempty,suggestion))==0
feature20(i)=-1;
else
feature20(i)=1;
end
end

我循环查找文件,并检查,但是当建议为空时它会返回错误的结果(1)

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

根据您的评论,听起来您希望输出是值的向量,每个文件一个。如果suggestion包含错误,则值为1;如果为空,则值为-1。

问题在于,由于suggestion是一个单元格数组,因此无法在每次迭代时使用布尔运算符==' . If you want a vector of outputs, you will also need to index feature20进行比较。这是一个潜在的解决方案。

听起来isempty(suggestion)给出了错误的答案,请尝试以下方法:

if sum(~cellfun(@isempty,suggestion))==0
    feature20(i)=-1;
else
    feature20(i)=1;
end