我写了一个函数来按照其中一个字段对玩家对象进行排序:
function [ sortedPlayers ] = sortPlayers(players)
%sorts player objects by rating
playersFields = fieldnames(players);
playersCell = struct2cell(players);
sz = size(playersCell);
playersCell = reshape(playersCell, sz(1), []);
playersCell = playersCell';
playersCell = sortrows(playersCell, -3);
playersCell = reshape(playersCell', sz);
sortedPlayers = cell2struct(playersCell, playersFields, 1);
end
运行时,我收到此错误:
Undefined function 'fieldnames' for input arguments of type 'double'.
Error in sortPlayers (line 4)
playersFields = fieldnames(players);
Error in getTeamPlayers (line 16)
teamPlayers = sortPlayers(teamPlayers);
Error in masterSimulate (line 13)
firstTeam = getTeamPlayers(team_dict, firstTeamName);
Error in mainMenu (line 25)
masterSimulate(team_dict);
Error in main (line 18)
mainMenu(team_dict, allPlayers);
我用谷歌搜索过,大多数时候这种类型的错误是由一个函数命名的,与其所在的文件不同。但是,“fieldnames”是一个内置函数。我跑:
which fieldnames -all
然后回来:
built-in (/Applications/MATLAB_R2011b.app/toolbox/matlab/datatypes/@struct/fieldnames) % struct method
built-in (/Applications/MATLAB_R2011b.app/toolbox/matlab/datatypes/@opaque/fieldnames) % opaque method
fieldnames is a built-in method % meta.PackageList method
fieldnames is a built-in method
...
知道这里发生了什么吗?我不认为我在sortPlayers
函数中改变了任何内容,所以我很遗憾:为什么它不再正常工作。
谢谢!
答案 0 :(得分:2)
看起来像你调用它时传递给函数的参数不是类struct
,用户定义的类或Java类,而是其他东西(double
可能?)。< / p>
要测试参数的类,只需在函数的开头添加:
fprintf(1, '"players" has the type "%s".\n', class(players));
然后再次调用您的函数(或运行为您调用它的应用程序)。