如何在 MATLAB 中保留断点的同时清除持久变量?

发布于 2024-08-27 06:08:31 字数 154 浏览 5 评论 0原文

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

clear all;

并且

clear functions;

都杀死断点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

断爱 2024-09-03 06:08:31

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

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

s=dbstatus;
save('myBreakpoints.mat', 's');
clear all
load('myBreakpoints.mat');
dbstop(s);

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);
掩饰不了的爱 2024-09-03 06:08:31

根据 RTBarnard 和 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 此处

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.

装迷糊 2024-09-03 06:08:31

如果@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')

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')
擦肩而过的背影 2024-09-03 06:08:31
s=dbstatus; % keep breakpoints
evalin('base','clear classes')
dbstop(s);

要复制到函数文件中(例如 myclearclasses)
这样就不需要临时 .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.

冰魂雪魄 2024-09-03 06:08:31

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

clear *;

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

clear *;
岁月静好 2024-09-03 06:08:31

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文