0

使用图形句柄,您可以执行以下操作:

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

如果这是使用普通句柄派生类完成的,则会发生此错误:

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

据我了解,图形句柄只是双打。自定义对象也可以这样表现吗?

4

1 回答 1

0

例子:

我的对象.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

请注意,对象a(1:2)是通过调用不带参数的默认构造函数来构造的。

另一种方法:

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

注意:处理图形 (HG) 不是使用已记录的MCOS类系统实现的,而是使用较旧且完全未记录的UDD类。

于 2013-07-06T17:52:57.510 回答