Reputation: 345
I am trying to write a script that counts the maximum number of consecutive rounds a genome is alive in a pool. I do this using the following code:
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
However, the last 5 rows (or rather calling: keys(livedlong)
or values(livedlong)
) yields the following error: Function 'subsindex' is not defined for values of class 'containers.Map'.
and I have no idea why. I was able to use the commands on the Map in the command window a while ago, but now I cannot do that either.
Upvotes: 1
Views: 1373
Reputation: 65430
You must have variables named keys
and/or values
in your workspace and MATLAB is trying to use your container.Map
instance as an index (by calling subsindex
) and failing.
Either remove those variables:
clear keys values
Or use the dot notation for invoking those methods
k = livedlong.keys();
v = livedlong.values();
This is another good reason to use a function rather than a script so the workspace of your current function isn't polluted by what was run beforehand.
Upvotes: 1