1

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!

2
  • Without seeing the function/script that calls sortPlayers, we can't determine why the argument it received is a double. Commented Nov 10, 2015 at 23:56
  • The error is because players is a double. You'll just have to trace back through your code to wherever it is defined or modified. Commented Nov 11, 2015 at 1:04

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, the last time 'sortPlayers' gets called, its argument is of type 'double' :( I've updated my question to include the full error message. Still not sure why what's passed in is of type 'double' and not 'struct'
@bclayman Is really difficult to figure out what is wrong with code only from the stack trace messages. One way is to configure MATLAB to stop on all errors. When the error occurs you can look in the context of all the functions on the stack. Look at "Examples and How To" here: mathworks.com/help/matlab/debugging-code.html
Just followed what they say in the link you provided. I get that players = [] and that players is of class double...very strange
@bclayman I'm sorry, but I realize that one cannot answer the question you asked as it is. Without the code for the getTeamPlayers one cannot see how the argument for sortPlayers is built in the first place. The thing is: your mistake is rather upstream sortPlayers, so the code from those functions is significant, while the code of sortPlayers is not (it just throws an error for expecting a type and not getting it). Do you want me to delete the answer? Also, I would recommend either changing the question or deleting it, because is not well defined as it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.