# Cannot convert double value 0.01 to a handle.Cannot convert double value 5 to a handle.

32 views (last 30 days)
Taha Sajid on 3 Mar 2021
Answered: Walter Roberson on 4 Mar 2021
Can someone please explain me in detail what does these handles mean! As theere solution is different everytime. Please tell me about it
James Tursa on 4 Mar 2021
I can't help unless I see the code. I could only make guesses as to what might be wrong.

Walter Roberson on 4 Mar 2021
format long g
fig = figure(1);
fig
fig =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [509 657 583 437] Units: 'pixels' Show all properties
class(fig) %notice it is a handle
ans = 'matlab.ui.Figure'
fig.Number %that has an associated integer
ans =
1
H1 = handle(1); %what happens if we convert 1 to handle?
H1
H1 =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [509 657 583 437] Units: 'pixels' Show all properties
isvalid(H1)
ans = logical
1
H1 == fig %is it the same as the handle we generated
ans = logical
1
H2 = handle(2); %what happens if we convert 2 to handle when no figure 2 exists?
H2
H2 = handle
ishandle(H2)
ans = logical
0
set(1, 'Visible', 'on') %works, because there IS a handle associated with double 1
try
set(2, 'Visible', 'on') %fails, because there is NO handle associated with double 2
catch ME
disp(ME)
end
MException with properties: identifier: 'MATLAB:class:InvalidHandle' message: 'Invalid or deleted object.' cause: {} stack: [7×1 struct] Correction: []
H1(1) = 1 %what if we assign a double into an existing handle
H1 =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [509 657 583 437] Units: 'pixels' Show all properties
try
H1(2) = 2 %and if we try with a number that does not represent a handle?
catch ME
disp(ME)
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
L1 = zeros(1,3)
L1 = 1×3
0 0 0
L1(1) = plot(rand(1,3)) %this still works!
L1 = 1×3
0.0048828125 0 0
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
try
L2(2) = 0.5
catch ME
disp(ME)
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)))
L2 =
0.0050048828125
L2(2) = 0.5
L2 = 1×2
0.0050048828125 0.5
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.