Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear persistent variables while keeping breakpoints in MATLAB?

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.

like image 434
Philipp Avatar asked Mar 25 '10 11:03

Philipp


2 Answers

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);
like image 174
RTBarnard Avatar answered Oct 03 '22 05:10

RTBarnard


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.

like image 30
Brandon Avatar answered Oct 03 '22 04:10

Brandon