Cannot convert double value 0.01 to a handle.Cannot convert double value 5 to a handle.
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Can someone please explain me in detail what does these handles mean! As theere solution is different everytime. Please tell me about it
3 commentaires
James Tursa
le 4 Mar 2021
I can't help unless I see the code. I could only make guesses as to what might be wrong.
Réponses (1)
Walter Roberson
le 4 Mar 2021
format long g
fig = figure(1);
fig
class(fig) %notice it is a handle
fig.Number %that has an associated integer
H1 = handle(1); %what happens if we convert 1 to handle?
H1
isvalid(H1)
H1 == fig %is it the same as the handle we generated
H2 = handle(2); %what happens if we convert 2 to handle when no figure 2 exists?
H2
ishandle(H2)
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
H1(1) = 1 %what if we assign a double into an existing handle
try
H1(2) = 2 %and if we try with a number that does not represent a handle?
catch ME
disp(ME)
end
Historically, graphics objects were represented to the user as double, and it was valid to do things like
L1 = zeros(1,3)
L1(1) = plot(rand(1,3)) %this still works!
and contrawise it was valid to do things like
L2 = plot(rand(1,4))
try
L2(2) = 0.5
catch ME
disp(ME)
end
and you can still do
L2 = double(plot(rand(1,3)))
L2(2) = 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Software Development Tools dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!