A safety net for MATLAB's clear command

版本 1.4.0.0 (9.4 KB) 作者: Michael Völker
intercept amok calls to clear() in interactive mode
57.0 次下载
更新时间 2015/8/17

Today I erased a substantial amount of data in an instant. I hate you, clear, I sincerely hate you...

So you do something in an interactive Unix Shell and type 'clear' to get rid of some annoying ouput. Then you switch to MATLAB, do the same and lose all your current work. Syntax madness... o_O

It feels fundamentally wrong that MATLAB's clear() assumes that 'no input' == 'everything'. This function is the workaround, effective only for interactive calls. I would like to apply this to script calls, too, but this breaks a lot of matlab's own code.

This function intercepts all calls of clear() (hence the name...) and decides what to do based on the given arguments and whether it was called from the command window or not.

in pseudo code:
===============================================================
IF < NO ARGUMENT, I.E. CLEAR EVERYTHING >

IF < IS INTERACTIVE >

< ASK USER EXPLICITLY IF THEY WANT TO ERASE EVERYTHING >

IF < NO, THEY DON'T > % <-- safety net
RETURN
END
END
END

<DEFAULT CLEAR CODE>
===============================================================

Note: 'clear all' or 'clear variables' will still clear everything immediately, because it was specifically asked to do so!

This relies on undocumented MATLAB/JAVA code to get the last command from the history. If the command fails (maybe very old or very new MATLAB), this is caught by try/catch and clear behaves completely like the builtin function. So, don't rely on it, just have it as a safety net!

Usage
=====

Save this m-file anywhere in your matlab path such that it has
precedence over the builtin implementation. That is, make sure
that
>> which clear
returns the path to this file.

image by Alex E. Proimos and taken from Wikimedia Commons:
https://commons.wikimedia.org/wiki/File:Paris_Tuileries_Garden_Facepalm_statue.jpg

引用格式

Michael Völker (2024). A safety net for MATLAB's clear command (https://github.com/VolMi/matlab-clear), GitHub. 检索来源 .

MATLAB 版本兼容性
创建方式 R2015a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Lighting, Transparency, and Shading 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

无法下载基于 GitHub 默认分支的版本

版本 已发布 发行说明
1.4.0.0

updated description, because whitespace changes everytime

1.0.0.0

要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库
要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库