Limiting the number of Matlab figure windows

32 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Adam Danz
Adam Danz il 18 Dic 2019
Modificato: Adam Danz il 18 Dic 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
if debugMode
% 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.')
end
end
% within your code
if debugMode
figure(1)
plot( . . .)
end
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.
  2 Commenti
Sandy Throckmorton
Sandy Throckmorton il 18 Dic 2019
I really like the figure numbering approach! Thanks!
Adam Danz
Adam Danz il 18 Dic 2019
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.

Accedi per commentare.

Più risposte (2)

Andreas Bernatzky
Andreas Bernatzky il 18 Dic 2019
Modificato: Andreas Bernatzky il 18 Dic 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...

Sandy Throckmorton
Sandy Throckmorton il 18 Dic 2019
I do use a lot of breakpoints, the command 'keyboard', try/catch statements, and dbstop; but for a lot of things, nothing beats visualizing code behavior. For example, checking whether I'm selecting the correct area in an image (e.g. did I put the indices in the right order), plotting quickly highlights whether I did it right or wrong whereas simply looking at index values, not so much.
  1 Commento
Bootstrap2110
Bootstrap2110 il 15 Mag 2023
I agree with you. I have the same issue. When code is big with lots of scripts calling each other internally its easy to miss disabling a few plots. I also wish there was a way to set the max numer of plots after which matlab can just ignore plotting.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by