The Problem with clc; clear; close all; ?

610 visualizaciones (últimos 30 días)
atharva aalok
atharva aalok el 17 de Oct. de 2021
Comentada: kunal goswami el 18 de Mzo. de 2022
A few days ago someone pointed out that using:
clc; clear; close all;
Is considered as a case of code smell and cargo cult programming.
But does that really have to be the case?
clc: cleans up the command window and now one can work without getting confused with the commands for previous runs
clear: erases the variables from previous runs this will reduce chances of error in subsequent runs and the programmer does not have to worry about unnecessary trash variables.
close all: closes all currently open figures. This can be very helpful during subsequent runs of the same script. If the figure from the previous run has not been closed then the subsequent run will plot the data on the already open figure. Which of course is a total waste.
What is the problem with using these commands?
  3 comentarios
per isakson
per isakson el 17 de Oct. de 2021
"clear: erases the variables from previous runs" use clearvars for that rather than clear , which does so much more.
kunal goswami
kunal goswami el 18 de Mzo. de 2022
thanks ;

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 17 de Oct. de 2021
My main point is that people will get into the habit of using this, and will also use it in functions.
Clear is almost never needed in normal use, as you can use functions to keep your workspace clean. Close all should also be avoided, as you should use explicit handles to make your code robust. If there is an unexpected close all in a function, that will be annoying.
The big exception is during debugging. If you're using a script to quickly check something, these are fine. That is why all Image Analyst's demos start with it, and why you probably won't find any of them in his functions (with the possible exception of clc).
  2 comentarios
atharva aalok
atharva aalok el 17 de Oct. de 2021
Thanks makes much more sense now.
Walter Roberson
Walter Roberson el 17 de Oct. de 2021
There are cases where clearing a variable can be warranted, if you are using large enough variables that you are likely to run across memory limits, and the variable is not being used in a tight loop.
Clearing a variable forces the optimizer to recompute whether a future reference to the name should be treated as undefined or as a function call. For example,
gamma = 1:10;
gamma(5)
clear gamma
gamma(5)
After the clear, gamma did not become undefined: it became a reference to a function.
Much of the time, MATLAB can make block-level predictions about whether any given name resolves to a variable or a function, but when you clear a variable it cannot do that anymore, so the code slows down.
Thus, if you have the memory, it is more efficient to leave the clearing to be automatic from returning from a function.
See my timing tests at https://www.mathworks.com/matlabcentral/answers/60240-what-is-the-difference-between-the-effect-of-clear-and-clearvars#answer_757137 -- which, by the way, indicate that clearvars can be much much slower than clear.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by