MATLAB:使用containers.Map混淆行为

时间:2015-10-27 22:16:57

标签: matlab

我在使用特定密钥访问该值时遇到了一些问题(我使用的是container.Map)。我已经设置了一个名为team_dict的地图,其外观如下:

{ 'Columbia' : 'www.columbia.com', 'Bates' : 'www.bates.com', ... }

我尝试做

url = team_dict(currentKey); 

访问关键字currentKey对应的Map中的值。

以下是相关代码:

allKeys = keys(team_dict);

%loop through all keys
for i = 1 : length(allKeys)
    %for current key (team name), getTeam
    currentKey = allKeys(i);
    disp(currentKey);
    url = team_dict(currentKey);
end

我收到此错误:

Error using containers.Map/subsref
Specified key type does not match the type expected for this container.

Error in project (line 27)
    teamPlayers = getTeam(team_dict(currentKey), currentKey);

奇怪的是,哥伦比亚'当我拨打disp(currentKey)时,打印出来正确。此外,在交互式提示中,当我这样做时

team_dict('Columbia')

我找回了正确的网址。

知道为什么会这样吗?

由于

1 个答案:

答案 0 :(得分:1)

由于currentKey = allKeys(i);返回键的单元格数组,当您获得disp(currentKey);时,您将拥有一个包含该键的单元格。

由于您的密钥都是字符串,url = team_dict(currentKey);仍然有效。但是currentKey会导致错误,因为currentKey = allKeys(i); 现在是字符串的单元格。

你要做的就是修改这一行:

currentKey = allKeys{i};

{{1}}
相关问题