Is there a way to clear all persistent variables in MATLAB functions, while keeping the breakpoints in the corresponding function files?
clear all;
and
clear functions;
both kill the breakpoints.
Unfortunately, clearing persistent variables also clears breakpoints, but there is a workaround.
After setting the breakpoints you want to retain, use the dbstatus
function to get a structure containing those breakpoints and then save that structure to a MAT file. After clearing variables, you can then reload those variables by loading the MAT file and using dbstop. Following is an example of performing this sequence of operations:
s=dbstatus;
save('myBreakpoints.mat', 's');
clear all
load('myBreakpoints.mat');
dbstop(s);
Building from RTBarnard's and Jonas's solutions, I came up with a script that avoids the need to save and load from a file. Note, however, that this does not clear the classes like Jonas's solution. I also close all the figures, since that's what I typically want to do when clearing everything. Here it is:
% Close all figures including those with hidden handles
close all hidden;
% Store all the currently set breakpoints in a variable
temporaryBreakpointData=dbstatus('-completenames');
% Clear functions and their persistent variables (also clears breakpoints
% set in functions)
clear functions;
% Restore the previously set breakpoints
dbstop(temporaryBreakpointData);
% Clear global variables
clear global;
% Clear variables (including the temporary one used to store breakpoints)
clear variables;
This script and some other Matlab utilities are on Github here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With