Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用matlab在文本文件中搜索特定于计数的单词?_Matlab_Text Processing_Text Parsing_Textscan - Fatal编程技术网

如何使用matlab在文本文件中搜索特定于计数的单词?

如何使用matlab在文本文件中搜索特定于计数的单词?,matlab,text-processing,text-parsing,textscan,Matlab,Text Processing,Text Parsing,Textscan,我需要在matlab中编写一个程序,在文本文件中搜索特定的关键字,然后计算文本中该特定单词的数量。matlab实际上有一个函数strfind,可以为您执行此操作。有关更多详细信息,请查看 文本文件的内容应存储在变量中: text = fileread('filename.txt') 首先了解如何使用Matlab读取文本文件,然后使用textscan函数将整个数据读取到单元格数组中,该函数是Matlab中的内置函数,并使用strcmp将数组的每个元素与特定关键字进行比较 我提供了一个函数,它将文

我需要在matlab中编写一个程序,在文本文件中搜索特定的关键字,然后计算文本中该特定单词的数量。

matlab实际上有一个函数
strfind
,可以为您执行此操作。有关更多详细信息,请查看

文本文件的内容应存储在变量中:

text = fileread('filename.txt')

首先了解如何使用Matlab读取文本文件,然后使用
textscan
函数将整个数据读取到单元格数组中,该函数是Matlab中的内置函数,并使用
strcmp
将数组的每个元素与特定关键字进行比较

我提供了一个函数,它将文件名和关键字作为输入,并输出文件中存在的关键字数

function count = count_word(filename, word)
count = 0;                  %count of number of specific words
fid = fopen(filename);      %open the file
c = textscan(fid, '%s');    %reads data from the file into cell array c{1}

for i = 1:length(c{1})
    each_word = char(c{1}(i));   %each word in the cell array
    each_word = strrep(each_word, '.', '');   %remove "." if it exixts in the word
    each_word = strrep(each_word, ',', '');   %remove "," if it exixts in the word

    if (strcmp(each_word,word))   %after removing comma and period (if present) from each word check if the word matches your specified word
        count = count + 1;        %increase the word count
    end
end
end

我投票把这个问题作为离题题来结束,因为它没有表现出任何努力,可能只是一个“做我的家庭作业”类型的问题。请编辑以显示您尝试过的内容以及具体不起作用的内容。