0

I have an enumeration defined somewhere:

classdef MyError

  enumeration

    Error0
    Error1
    Error2

  end

end

Then, I'm trying a simple call to ismember with different version of Matlab.

R2012a:

>> enums = enumeration('MyError');
>> ismember(MyError.Error0,enums)

ans =

     1

R2013b:

>> enums = enumeration('MyError');
>> ismember(MyError.Error0,enums)
Undefined function 'sort' for input arguments of type 'MyError'.
Error in ismember>ismemberClassTypes (line 711)
    sort(ab(1));
Error in ismember>ismemberR2012a (line 490)
            lia = ismemberClassTypes(a,b);
Error in ismember (line 57)
    [varargout{1:max(1,nargout)}] = ismemberR2012a(A,B); 

I'm looking at the code issuing the error and I cannot understand what is the purpose of the call to 'sort' because its input is a scalar value and the output is not stored. Given that calling ismember on enumerated value is rather common task, I'm surprised to see such regressions.

Any ideas for a work-around?

Eric Salemi
  • 119
  • 9
  • `sort` is simply used as part of the algorithm behind `ismember` to accelerate things. The error message however also suggests, that there seems to be some special treatment - given that there's a subfunction called `ismember2012a`. – sebastian Dec 10 '13 at 12:48
  • The behaviour of `ismember` changed, if you want the old behaviour set the legacy flag: http://stackoverflow.com/questions/20397910/matlab-behavior-of-the-unique-function/20398118#20398118 – Daniel Dec 10 '13 at 13:36
  • AFAIK the 'legacy' option has to do either with ordering of the LOCB output argument or class combination (see inline help). I'm not doing any of this, so even if the workaround works in my case, Mathworks did introduce a bug. – Eric Salemi Dec 10 '13 at 14:02
  • I guess that a combination of 2 times the same class is also a combination. You can use the `legacy` flag. The `rows` flag also seems to do the trick, but that feels a bit like a hack to me. -- Note that the call to `sort` may seem overkill but it is actually what makes `ismember` so efficient. – Dennis Jaheruddin Dec 10 '13 at 14:31
  • @DennisJaheruddin Did you notice the output of the `sort` call is not used? How can that make `ismember` efficient? – Eric Salemi Dec 10 '13 at 17:09
  • @EricSalemi Actually I didn't see any output left unused. But then again I do have a different version. If you are really sure the line is not used you can try commenting it out? If that helps you surely can file a bug report. – Dennis Jaheruddin Dec 11 '13 at 10:36

2 Answers2

0

I can't test this locally, but you might want to check if things work with a slightly modified class definition:

classdef MyError < uint32

  enumeration

    Error0 (0)
    Error1 (1) 
    Error2 (2)

  end

end

Subclassing any numeric type should get you the sort-method. This is still not a real fix or explanation, but might be the best workaround.

sebastian
  • 9,526
  • 26
  • 54
  • Yes, I thought about this, but the real class implementation I'm using has properties, and Matlab does not allow properties when when a builtin class is used as parent. – Eric Salemi Dec 10 '13 at 14:38
0

Unfortunately it seems to be a feature, not a bug.

In the 'old' version of matlab, the help file states:

In a future release, the behavior of ismember will change including:
  - occurrence of indices in LOCB will switch from highest to lowest
  - tighter restrictions on combinations of classes

In order to see what impact those changes will have on your code, use:

   [LIA,LOCB] = ismember(A,B,'R2012a')
   [LIA,LOCB] = ismember(A,B,'rows','R2012a')

If the changes in behavior adversely affect your code, you may preserve
the current behavior with:

   [LIA,LOCB] = ismember(A,B,'legacy')
   [LIA,LOCB] = ismember(A,B,'rows','legacy')

If I try it with the R2012a flag enabled I indeed get the error that you mention. Hence I suppose the given advice will work for you. Include the legacy flag and likely that will do the trick.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • Still, as the OP commented, the document change in behaviour has no relation whatsoever to the observed error. Though of course the bug presumably came along while introducing the new behaviour. – sebastian Dec 10 '13 at 14:32
  • The 'legacy' trick works for me, but that does not mean that Mathworks did not introduce a bug... there is nothing in the old help files that specifically mention the error I'm witnessing. I will file a ticket with Mathworks and see what they have to say about it. – Eric Salemi Dec 10 '13 at 14:34