Question

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.

Was it helpful?

Solution

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);

OTHER TIPS

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 there is data in @directories, you can still use the method that RTBarnard proposes

s=dbstatus('-completenames');
save('myBreakpoints.mat','s');
%# if you're clearing, you may as well just clear everything
%# note that if there is stuff stored inside figures (e.g. as callbacks), not all of 
%# it may be removed, so you may have to 'close all' as well
clear classes 
load('myBreakpoints.mat')
dbstop(s);

%# do some cleanup
clear s
delete('myBreakpoints.mat')
s=dbstatus; % keep breakpoints
evalin('base','clear classes')
dbstop(s);

To be copied in a function file (example myclearclasses) This way no need for temporary .mat file.

It's simple, you should use * as regexp to find all variables. It'll clean whole workspace and breakpoints'll exist.

clear *;

I came up with a quick solution for this using preferences and the others' answers:

setpref('user', 'breakpointBackup', dbstatus('-completenames'));
clear all;
clear import;
clear java;
dbstop(getpref('user', 'breakpointBackup'));

The advantage of this approach is it's very clean (i.e. no temporary file) and clears everything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top