This code snippet causes Matlab to crash

1 次查看(过去 30 天)
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 个评论
Rik
Rik 2017-12-22
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 2017-12-22
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.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2017-12-22
(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 个评论
Rik
Rik 2017-12-22
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 2017-12-22
Nice and insightful explanations. Thanks a lot, Rik!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by