Matlab 检查if-else语句并循环文件
我想循环浏览文件并检查每个文件是否包含任何拼写错误Matlab 检查if-else语句并循环文件,matlab,matlab-figure,Matlab,Matlab Figure,我想循环浏览文件并检查每个文件是否包含任何拼写错误 If yes then return 1 else -1. 现在,我可以检查1个文件是否存在拼写错误。但它不能循环超过1次,如果get error else-1,则返回1 检查拼写.m文件 function suggestion = checkSpelling(word) h = actxserver('word.application'); h.Document.Add; correct = h.CheckSpelling(word);
If yes then return 1 else -1.
现在,我可以检查1个文件是否存在拼写错误。但它不能循环超过1次,如果get error else-1,则返回1
检查拼写.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)
根据您的评论,听起来您希望输出是一个值向量,每个文件一个。如果建议包含错误,则值为1;如果建议为空,则值为-1 问题在于,由于
suggestion
是一个单元格数组,因此无法使用布尔运算符=='对其进行比较。如果需要输出向量,还需要为每次迭代编制
feature20`索引。这里有一个潜在的解决方案
听起来好像是空的(建议)
给出了错误的答案,所以试试这个:
if sum(~cellfun(@isempty,suggestion))==0
feature20(i)=-1;
else
feature20(i)=1;
end
不清楚。您想要文件中所有错误的集合,还是它遇到的第一个错误的集合?
但是它循环了超过1个
-您确切地看到了什么问题?一点后就停了吗?它会给你一个错误吗?不需要收集所有的错误。建议将返回我是否文章gt错误,如果建议是空的,这意味着没有错误,如果建议得到返回我的建议词,这意味着得到了错误。因此,我必须检查建议是否为空,如果为空,则返回-1,否则返回1。这是一个文件或一篇文章。有10个have和10个article循环。最后,输出将是feature20=[1-11-1-1]同样,我不确定为什么suggestion==0
会起作用,因为它应该是一个从cellfun
yes中产生的单元格数组,“?”未定义的函数或方法“eq”,用于“cell”类型的输入参数。如果suggestion==0,则==>f20中出现错误。如何修改它?是,但是当我的建议为空时,它会返回错误的结果,建议为空返回1~我知道您的检查是正确的。但当我的建议为空时,它会返回错误的结果。有什么想法吗?仍然返回与刚才相同的结果~@user3340270我会确保suggestion
的单个元素实际上是空的。没有您的数据,我无法进行任何实际调试。我只是在文件2中写了一些没有拼写错误的正确句子,在文件1中出现了拼写错误。我已经打印了我的建议结果,如上所述。建议1:出现错误,建议2:无错误