fig
fig =
Figure (1) with properties:
Number: 1
Name: ''
Color: [1 1 1]
Position: [509 657 583 437]
Units: 'pixels'
Show
all properties
H1
H1 =
Figure (1) with properties:
Number: 1
Name: ''
Color: [1 1 1]
Position: [509 657 583 437]
Units: 'pixels'
Show
all properties
end
MException with properties:
identifier: 'MATLAB:class:InvalidHandle'
message: 'Invalid or deleted object.'
cause: {}
stack: [7×1 struct]
Correction: []
H1(1) = 1
H1 =
Figure (1) with properties:
Number: 1
Name: ''
Color: [1 1 1]
Position: [509 657 583 437]
Units: 'pixels'
Show
all properties
end
MException with properties:
identifier: 'MATLAB:graphics:CannotConvertDoubleToHandle'
message: 'Cannot convert double value 2 to a handle'
cause: {}
stack: [7×1 struct]
Correction: []
Historically, graphics objects were represented to the user as double, and it was valid to do things like
and contrawise it was valid to do things like
L2 = plot(rand(1,4))
L2 =
Line with properties:
Color: [0 0.447 0.741]
LineStyle: '-'
LineWidth: 0.5
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1 2 3 4]
YData: [0.658017094644957 0.881416534115599 0.405806040040171 0.216775714080096]
ZData: [1×0 double]
Show
all properties
end
MException with properties:
identifier: 'MATLAB:graphics:CannotConvertDoubleToHandle'
message: 'Cannot convert double value 0.5 to a handle'
cause: {}
stack: [7×1 struct]
Correction: []
and you can still do
L2 = double(plot(rand(1,3)))
Thus, the particular error comes about in old code that happened to assign a graphics object to a variable thinking that graphics objects are represented by doubles (which they used to be), and then assigns a double (not intended to be a graphics object) into the same array.
You can work around this kind of problem by carefully using double() and handle(), but it is usually best to separate out the graphics objects from the non-objects.