Using CLEAR ALL usually decreases code performance and is often unnecessary

74 ビュー (過去 30 日間)
MUNKHBOLOR BAIGALI
MUNKHBOLOR BAIGALI 2020 年 10 月 28 日
コメント済み: Peter O 2020 年 10 月 28 日
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 件のコメント
Stephen23
Stephen23 2020 年 10 月 28 日
編集済み: Stephen23 2020 年 10 月 28 日
"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 2020 年 10 月 28 日
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 件のコメント
Stephen23
Stephen23 2020 年 10 月 28 日
"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.
Peter O
Peter O 2020 年 10 月 28 日
Excellent points, Stephen!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by