2015-10-27 129 views
0

我在使用特定键(我使用containers.Map)访问值时遇到了一些麻烦。我已经成立了一个名为地图team_dict看起来像:MATLAB:容器混淆行为。地图

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

我尝试做

url = team_dict(currentKey); 

对应于关键currentKey地图来访问值。

下面是相关代码:

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') 

我找回正确的URL。

任何想法为什么会发生这种情况?

感谢

回答

1

由于allKeys = keys(team_dict);返回键的单元阵列,当你得到currentKey = allKeys(i);你将有包含密钥的单元格。

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

,你所要做的仅仅是修饰这一行:

currentKey = allKeys(i); 

currentKey = allKeys{i}; 
+1

你介意拟订之间{的差异}和[]或指着我的多方向关于它的信息?忽略,只是发现:http://www.mathworks.com/help/matlab/matlab_prog/what-is-a-cell-array.html – bclayman

+0

是的,这是链接。如果你的问题得到解决,你介意接受答案吗? – scmg