Reputation: 9355
I've written a function to sort player objects by one of their fields here:
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
When it runs, I get this error:
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);
I've googled around and most of the time this type of error is caused by a function being named differently from the file its in. However, 'fieldnames' is a built-in function. I run:
which fieldnames -all
and get back:
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
...
Any idea what's going on here? I don't think I changed anything within my sortPlayers
function so I'm pretty lost re: why it's no longer working properly.
Thanks!
Upvotes: 1
Views: 1337
Reputation:
Seems like the argument that you pass to your function when you call it is not of class struct
, a user defined class or a Java class, but something else (double
maybe?).
To test the class of the argument, just add at the beginning of the function:
fprintf(1, '"players" has the type "%s".\n', class(players));
and then call your function again (or run the application that calls it for you).
Upvotes: 2