15

有没有办法清除 MATLAB 函数中的所有持久变量,同时将断点保留在相应的函数文件中?

clear all;

clear functions;

都杀死断点。

4

6 回答 6

15

不幸的是,清除持久变量也会清除断点,但有一种解决方法。

设置要保留的断点后,使用该dbstatus函数获取包含这些断点的结构,然后将该结构保存到 MAT 文件中。清除变量后,您可以通过加载 MAT 文件并使用 dbstop 重新加载这些变量。以下是执行此操作序列的示例:

s=dbstatus;
save('myBreakpoints.mat', 's');
clear all
load('myBreakpoints.mat');
dbstop(s);
于 2010-03-25T11:34:26.473 回答
10

从 RTBarnard 和 Jonas 的解决方案构建,我想出了一个脚本,它避免了从文件保存和加载的需要。但是请注意,这并不能清除 Jonas 解决方案之类的类。我还关闭了所有数字,因为这是我在清除所有内容时通常想要做的事情。这里是:

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

该脚本和其他一些 Matlab 实用程序在Github 上。

于 2011-04-12T23:21:28.163 回答
3

如果@directories 中有数据,你仍然可以使用RTBarnard提出的方法

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')
于 2010-03-25T16:53:15.353 回答
1
s=dbstatus; % keep breakpoints
evalin('base','clear classes')
dbstop(s);

要在函数文件中复制(例如 myclearclasses) 这种方式不需要临时 .mat 文件。

于 2013-02-05T13:48:01.947 回答
0

很简单,您应该使用 * 作为正则表达式来查找所有变量。它会清理整个工作区,并且会存在断点。

clear *;
于 2014-03-22T10:29:32.563 回答
0

我想出了一个使用首选项和其他人的答案的快速解决方案:

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

这种方法的优点是它非常干净(即没有临时文件)并且可以清除所有内容。

于 2015-05-13T17:56:36.687 回答