matlab错误:功能' subsindex'没有为class' containers的值定义.Map'

时间:2016-12-12 13:23:15

标签: matlab dictionary

我正在尝试编写一个脚本来计算基因组在池中存活的最大连续轮数。我使用以下代码执行此操作:

dd = dir('*.csv');

fileNames = {dd.name}; 

data = cell(numel(fileNames),2);
data(:,1) = regexprep(fileNames, '.csv','');

for i = 1:numel(fileNames)    
   data{i,2} = dlmread(fileNames{i});
end

cc = distinguishable_colors(numel(fileNames));                % get better colormap

livedlong = containers.Map;                                   % contains all the streaks

for k = 1:numel(fileNames)

    strat = data{k,1}(10:end); % get strategy name
    XY = data{k,2};            % get data
    X = XY(:,1);               % get rounds
    Y = XY(:,2);               % get #tiles
    streak = 1;                % set streak counter to 1
    longestStreak = streak;    % set longestStreak to 1        


    %%% Calculate the streaks!
    for l = 1:(numel(X)-1)
        if ((X(l)+1) == X(l+1))
            streak = streak + 1;
            if (streak > longestStreak)
                longestStreak = streak;
            end
        else
            streak = 1;
        end
    end

    livedlong(strat)=longestStreak;              % save the streaks

end

k = keys(livedlong);
v = values(livedlong);
for i = 1:length(livedlong)
    plot(k{i}, v{i}, 'o', 'color', cc(i,:))
end

但是,最后5行(或者更确切地说是:keys(livedlong)values(livedlong))会产生以下错误:Function 'subsindex' is not defined for values of class 'containers.Map'.我不明白为什么。我刚才能够在命令窗口中使用Map上的命令,但现在我也做不到。

1 个答案:

答案 0 :(得分:1)

您的工作区中必须包含名为keys和/或values的变量,MATLAB正在尝试将您的container.Map实例用作索引(通过调用subsindex)和失败。

删除这些变量:

clear keys values

或使用点符号调用这些方法

k = livedlong.keys();
v = livedlong.values();

这是使用函数而不是脚本的另一个好理由,因此当前函数的工作空间不会被预先运行的内容污染。