- Use clc to clean up the command window output. This command has no effect on the code you run. If you print things to the screen, they'll start from a clean command window.
- Use close all to clean up any open windows (e.g. plots) before running a script. MATLAB by default will draw a new plot into either the active figure (e.g. Figure 1), or create a new figure if there isn't one available. The default behavior is to overwrite the existing plot, but if your script uses an axis hold and you don't clear it, or if you have subplots in the figure, you may see plots that contain old information or traces in the wrong spot.
- Use clear to remove variables and definitions from the workspace. A script operates on MATLAB's main workspace, so there may be unexpected behavior if you've redefined a function or variable in the workspace outside of the script. For instance (and for example only--this is generally bad practice), you could redefine sin = @(x) x+2 in a previous script that leaves it in the workspace. Calling a new script without clearing first will cause the new script to use this function instead of the transcendental it is expecting and you'll get an incorrect result.
Using CLEAR ALL usually decreases code performance and is often unnecessary
104 views (last 30 days)
Error : Using CLEAR ALL usually decreases code performance and is often unnecessary
It get an error like this. Should I delete clear all code? or Should I ignore this error?
Why should I use clear all, close and clc before i write function?
Peter O on 28 Oct 2020
It sounds like you're seeing this in the Editor's Linter coding tips? These are all "clear the slate" commands,
You can safely ignore the clear all warning if you'd like, but generally the reason it recommends against "clear all" is that you remove some of the just-in-time-compilation steps that MATLAB caches for performance. Using clear (without the all) will generally erase all the variables and functions to give you a clean starting point but keep some of the under the hood optimizations. An exception to ignoring it is when you're tweaking global variables or doing class development. In those cases, you might want to be explicit about resetting the variable or class definition.
Hope this helps!