MatLab映射-指定的键类型不匹配

MatLab映射-指定的键类型不匹配,matlab,Matlab,我正在尝试制作一个程序(用于家庭作业),它读取一个文件,然后计算每个单词的使用次数。为了有效地解决这个问题,我决定将所有唯一的单词映射到键,然后在每次出现单词时增加键值 function [] = problem2 file_open = fopen('austen.txt'); complete_string = textscan(file_open, '%s'); numel(complete_string{1,1}) unique_words = uni

我正在尝试制作一个程序(用于家庭作业),它读取一个文件,然后计算每个单词的使用次数。为了有效地解决这个问题,我决定将所有唯一的单词映射到键,然后在每次出现单词时增加键值

function [] = problem2
    file_open = fopen('austen.txt');
    complete_string = textscan(file_open, '%s');

    numel(complete_string{1,1})

    unique_words = unique(complete_string{1,1});
    length(unique_words);
    frequency = zeros(numel(unique_words), 1);

    found_frequency = containers.Map(unique_words, frequency);

    for i=1:numel(complete_string{1,1})
       found_frequency(complete_string{1,1}(i)) = found_frequency(complete_string{1,1}(i))+1; 
    end

    fclose(file_open)

遗憾的是,这段代码不起作用。当行到达increment时,我收到一个错误,指出“指定的键类型与此容器预期的类型不匹配”,这对我来说毫无意义-我使用字符串作为键。关于我为什么会收到这个错误,有什么想法吗?

问题在于使用单元格类型-complete_String{1,1}(I)实际上会返回一个单元格而不是字符串(尽管按照规范)。用char(*)包装,效果很好。

这是我在这个脚本中使用的文件,如果任何人想重现警告,都可以使用。