0

With graphics handles, you can do this:

>> a = nan(1,5)

a =
   NaN   NaN   NaN   NaN   NaN

>> a(3) = line([1 2],[1 2])

a =
       NaN       NaN    0.0042       NaN       NaN

>> find(~isnan(a),1,'first')

ans =
     3

If this is done with a normal handle-derived class, this error happens:

>> a(3) = MyObject(1,1,1)
The following error occurred converting from MyObject to double:
Error using double
Conversion to double from MyObject is not possible.

As far as I understand, graphics handles are just doubles. Can custom objects also behave this way?

Amro
  • 123,847
  • 25
  • 243
  • 454
Brian
  • 3,453
  • 2
  • 27
  • 39
  • related questions: http://stackoverflow.com/q/6806344/97160, http://stackoverflow.com/q/2510427/97160 – Amro Jul 06 '13 at 17:39
  • These posts do not answer the question I am asking, which has to do specifically with being able to find(~isnan(x)) – Brian Jul 06 '13 at 18:26
  • @BBrock: perhaps the cell-array example I posted can help, you would use `~cellfun(@isempty, a)` instead of ISNAN to test for non-empty cells – Amro Jul 06 '13 at 18:37
  • @Amro: Thanks for the suggestion--I know I can use cell arrays for this purpose, but it would be particularly convenient for me to have functionality identical to the functionality offered by graphics handles...so I'm kind of guessing at this point that this is not possible? – Brian Jul 07 '13 at 06:38
  • 1
    @BBrock: I'm afraid that's not possible, you cannot make your class return objects as numeric handles. You should be aware that handle graphics (HG) are not implemented using the documented [MCOS](http://www.mathworks.com/help/matlab/object-oriented-programming.html) class system, rather using the older and completely undocumented UDD classes: http://undocumentedmatlab.com/blog/introduction-to-udd/ – Amro Jul 07 '13 at 06:51
  • this seems related: http://stackoverflow.com/questions/15298859/extending-matlab-uicontrol – Amro Jul 07 '13 at 06:56
  • Thank you, UDD/MCOS class information answers my question. Well I'm sad that I can't really do what I wanted to, but your alternatives are valid. I will accept your answer but can you edit it to include the information in this comment? Thanks, – Brian Jul 07 '13 at 14:56

1 Answers1

0

Example:

MyObject.m

classdef MyObject < handle
    properties
        x
    end
    methods
        function obj = MyObject(x)
            if nargin < 1, x = NaN; end
            obj.x = x;
        end
    end
end

MATLAB

>> a = MyObject.empty(0,3)
a = 
  0x3 MyObject array with properties:

    x

>> a(3) = MyObject(10)
a = 
  1x3 MyObject array with properties:

    x

>> a(3)
ans = 
  MyObject with properties:

    x: 10
>> a(1)
ans = 
  MyObject with properties:

    x: NaN

Note that the objects a(1:2) were constructed by calling the default constructor with no arguments.

An alternative way:

>> a = cell(1,3)
a = 
    []    []    []
>> a{3} = MyObject(10)
a = 
    []    []    [1x1 MyObject]

Note: handle graphics (HG) are not implemented using the documented MCOS class system, rather using the older and completely undocumented UDD classes.

Amro
  • 123,847
  • 25
  • 243
  • 454