使用图形句柄,您可以这样做:
>> 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.
据我所知,图形处理只是双打。自定义对象也可以这样做吗?
答案 0 :(得分:0)
示例:
classdef MyObject < handle
properties
x
end
methods
function obj = MyObject(x)
if nargin < 1, x = NaN; end
obj.x = x;
end
end
end
>> 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]