This code snippet causes Matlab to crash

The below code snippet causes Matlab to crash repeatedly, and can anyone tell how to fix this issue? Thanks.
while 1
figure; plot(rand(100))
pause(3)
close all
end
My environment is Matlab R2017a on RHEL 6.9.

2 commentaires

Rik
Rik le 22 Déc 2017
Why not use explicit handles and avoid messy opening and closing? It shouldn't crash, so for that you should contact support, but the code itself shouldn't be necessary.
Stephen23
Stephen23 le 22 Déc 2017
YS's "Answer" moved here:
@ Rik: OK, then treat this code snippet as my bug report... Any idea how to solve or bypass this tricky bug of Matlab? Really has been bothering me for a long while.

Connectez-vous pour commenter.

 Réponse acceptée

Rik
Rik le 22 Déc 2017
(Please use the comment field for responses, instead of the answer field)
On R2017b on 64 bit W10 I can't reproduce this (not with run section, nor by executing as script). As I said, use explicit handles and avoid closing and clearing everything, as that can lead to unexpected results.
f=figure;
ax=axes('Parent',f);
while 1
cla(ax);
plot(ax,rand(100));
pause(3)
end

5 commentaires

YS
YS le 22 Déc 2017
Thanks. Your code snippet provides a way to bypass my issue.
Could you please elaborate a bit more on "use explicit handles and avoid closing and clearing everything, as that can lead to unexpected results"? Why explicitly closing/clearing everything might lead to unexpected results?
Stephen23
Stephen23 le 22 Déc 2017
Modifié(e) : Stephen23 le 22 Déc 2017
@YS: these are two related pieces of very good advice:
  1. Always obtain and use explicit handles for all graphics objects that you use (figures, axes, lines, patches, images, etc). This makes code much more robust and reduces pointless bugs (like the one you have). Never assume that the current figure/axes/etc are the ones that you need to access.
  2. Do not close (and clear) everything brutally. Experienced MATLAB users will typically create one (or a few) figures/axes and simply update their contents. This makes code more robust and efficient, and is trivial to achieve following point 1.
YS
YS le 22 Déc 2017
@ Stephen: Thanks for your advices. Points taken for sure. However, it still confuses me a lot why explicitly closing graphs might lead to "pointless" bugs. Would appreciate it very much if some in-depth information regarding this can be shared.
Rik
Rik le 22 Déc 2017
I don't understand why Matlab would crash with this code, but 'bugs' can have a broader meaning as 'unexpected behavior'.
If you open a figure somewhere in your code, and your user switches to another figure during execution, your code could assume the current figure is the figure to write all the plots to. This will then lead to that second figure being overwritten and the first one have outdated contents.
The close all you were using is an implicit close, as it will close unspecified figures, close(f) (or cla(ax) for that matter) is explicit in what object should be targeted. The latter will never lead to overwriting of secondary figures that the user selected half-way through execution of some code.
In short, bugs that lead to Matlab crashing are always something to look out for and might be un-preventable, while bugs that lead to unexpected behavior are avoidable by using handles to graphics objects.
YS
YS le 22 Déc 2017
Nice and insightful explanations. Thanks a lot, Rik!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Tags

Question posée :

YS
le 22 Déc 2017

Commenté :

YS
le 22 Déc 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by