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?
clear all
close all
Why should I use clear all, close and clc before i write function?
  1 Comment
Stephen23 on 28 Oct 2020
Edited: Stephen23 on 28 Oct 2020
"Why should I use clear all, close and clc before i write function?"
You shouldn't.
Why should a function unneccesarily clear everything from memory (including compiled functions) and close all open figures and clear away all of that useful information that you have just been working on in the command window?
These are commands that should be used by hand when required, not brutally called "before i write function"
Does calling sqrt close all of your figures and clear the command window? Then why should your function? Your function should focus on performing some specific functionality, and not involve itself with other unrelated actions.

Sign in to comment.

Answers (1)

Peter O
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,
  • 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.
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!

Sign in to comment.


Find more on Loops and Conditional Statements in Help Center and File Exchange





Community Treasure Hunt

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

Start Hunting!