MATLAB Answers

Using CLEAR ALL usually decreases code performance and is often unnecessary

조회 수: 19(최근 30일)
MUNKHBOLOR BAIGALI
MUNKHBOLOR BAIGALI 28 Oct 2020
댓글: Peter O 28 Oct 2020
Hello.
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
clc
Why should I use clear all, close and clc before i write function?

  댓글 수: 1

Stephen Cobeldick
Stephen Cobeldick 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.

댓글을 달려면 로그인하십시오.

답변(1개)

Peter O
Peter O 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!

  댓글 수: 2

Stephen Cobeldick
Stephen Cobeldick 28 Oct 2020
"Use clear to remove variables..."
Better: clearvars
"The default behavior is to overwrite the existing plot...you may see plots that contain old information or traces in the wrong spot."
The robust approach is to write code using explicit graphics handles, in which case this becomes moot.
"You can safely ignore the clear all warning if you'd like..."
I do not recommend ignoring warnings provided by the MATLAB Editor. Much better to improve the code or explicitly ignore the warning on that line with a comment explaining why.

댓글을 달려면 로그인하십시오.

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by