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

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일

0 개 추천

(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

YS
YS 2017년 12월 22일
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 2017년 12월 22일
편집: Stephen23 2017년 12월 22일
@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 2017년 12월 22일
@ 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 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개)

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

태그

질문:

YS
2017년 12월 22일

댓글:

YS
2017년 12월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by