Limiting the number of Matlab figure windows
20 views (last 30 days)
Show older comments
Commented: Adam Danz on 18 Dec 2019
There probably isn't a solution to this (other than to stop messing things up), but I figured it's worth an ask. Often while debugging my code, I'll insert various figure/plot commands to see what is going on with the data and make sure I'm getting the right answer. Ideally, once I think I've got things working, I'll comment all of these out or delete them, but sometimes I'll miss a few with frustrating results. In older versions of Matlab, it would crash after say 20 figures. But now (with 2019a) it seems perfectly happy to freeze all processes on my computer while it dutifully attempts to plot 600+ figures. Is there some internal limit on figure windows that can be set to curb this behavior?
Edited: Adam Danz on 18 Dec 2019
"Is there some internal limit on figure windows that can be set"
Not that I'm aware of. Matlab will crash when it reaches memory capacity.
Here are some suggestions to manage the debugging figures situation.
Set a debug flag that conditionally creates the figures
Instead of manually commenting/de-commenting the debugging sections of your code, set an internal flag at the top of your file that indicates when the debugging sections should be accessed. Then put all of the debugging sections into conditional statements. Here's a template.
% Top of code
debugMode = true;
% Confirm that user wants to use debug mode
% This is optional, it's to let the user know when you're in debug mode.
resp = questdlg('Do you want to continue with debug mode?',mfilename, 'Yes','No','Yes')
if isempty(resp) || strcmpi(resp,'No')
error('Debug mode cancelled.')
% within your code
plot( . . .)
Set the figure number
Instead of creating a next figure via figure(), set the figure number using figure(1), figure(2), etc. This way if you have 10 debugging figures and the code is repeated, those 10 figures will be overwritten instead of an additional 10 figures being created.
Use clf() instead of creating new figures
The clf() command either clears the current figure or creates a new one if a figure doesn't already exist. By using clf() instead of figure(), you're always reusing the same figure instead of creating multiple figures. However, if your code creates multiple debugging figures, each one will be overwritten.
Note that you can combine the figure numbering idea with the debug-flag idea. That way when you want to turn on/off the debugging features, you just need to set the flag at the top of your code.
More Answers (2)
Andreas Bernatzky on 18 Dec 2019
Edited: Andreas Bernatzky on 18 Dec 2019
You should consider debugging without plotting and use breakpoints instead.
I do not know the maximum limit of possible plots because that depends on the data you plot (arraySize for example). But in theory I think the limitiation of possible plots is the (dynamic) memory your computer has left for usage. And freezing your computer is exactly what happens if your computer uses all of its memory...
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!