Using the clear command in functions?
Mostrar comentarios más antiguos
So I was practicing functions. . . My habit for writing scripts is to put clear, clc, close all before every section, unless variables are carrying over, to keep everything "cleaned up".
My question is, when I put clear in a function it gives me a statement saying it reduces code performance and is often not needed.
Why is this the case for functions but not scripts?
Also in my example function below, any reason why I am getting an error when calling it? (I used clear here to show what I mean)
function m_kg=lb2kg_ZLKW(w_lb)
% converts weight in lbs to mass in kg
clc, clear
m_kg=1/2.2.*w_lb;
end
% code to call function: w_lb=0:1:100; m_kg=lb2kg_ZLKW(w_lb);
2 comentarios
"My habit for writing scripts is to put clear, clc, close all before every section, unless variables are carrying over, to keep everything "cleaned up"."
Habits like this just need to be unlearned later. This is what you are finding out now.
Much better habits:
- Use functions for any code that is used for more than one day.
- Avoid CLEAR & CLOSE & CLC in code.
"My question is, when I put clear in a function it gives me a statement saying it reduces code performance and is often not needed."
Because it reduces code performance and is often not needed.
"Why is this the case for functions but not scripts?"
Many experienced users would argue that this is always the case.
"Also in my example function below, any reason why I am getting an error when calling it?"
Because you have learned some bad habits (e.g. automatically writing CLEAR & CLOSE & CLC at the start of everything that you write):
Why are you going to CLEAR the workspace of a function? At best it does absolutely nothing (because the workspace is fresh and new anyway) and at worst it will cause pointless bugs (like your example here, where you clear the input arguments and thus make the function completely uncallable).
CLEAR should be called sparingly, most often only from the command line when trying things out. Only rarely does CLEAR need to be called in code... and even then, you are probably really wanting to call CLEARVARS and not CLEAR.
CLC should also be avoided: when you call SIN(PI) does it clear the command window? (hint: no) Then why does your function need to? Ditto for CLOSE. Do not mix up different functionalities in one function.
Spaceman
el 14 de Mzo. de 2024
Respuesta aceptada
Más respuestas (1)
KSSV
el 14 de Mzo. de 2024
Becuase
clc, clear
clears your input variables which are saved in the workspace. You need not to use it inside the function.
6 comentarios
Spaceman
el 14 de Mzo. de 2024
Rik
el 19 de Mzo. de 2024
You should question why you are using these. What do you want to achieve? They do something specific. Did you read the documentation to figure out if you actually want that to happen?
And what is the difference between a comma and a semicolon in Matlab?
Can you now answer your own questions? Feel free to post a comment if not.
Spaceman
el 21 de Mzo. de 2024
Rik
el 21 de Mzo. de 2024
Close, but some details (which can sometimes be relevant) are not quite correct.
A statement can be terminated by a newline, a comma, or a semicolon. if a variable is assigned, the results are printed to the command window for newline and comma, but not semicolon.
Creating arrays is a different context, where newline and semicolon denote a new row, while comma and space denote a new column.
A space does not separate two commands, but instead enters the command argument mode. This is often used in combination with the cd command to change the active directory (although you should only really need to do so when starting a GUI, and even then only once, and even then it may not be required). That is also why you see the colors change in the editor:
cd,foo
cd bar
As for clear:
If you pause execution in a function (e.g. with a breakpoint) and then use clear, you will clear the variables in that workspace, not the base workspace. That makes clear a powerfull, but dangerous tool. But there is a further nuance: it doesn't just clears variables. If you read the documentation closely, you will notice more categories. Matlab can optimize functions the first time (or first few times) they run. This investment pays out if you run functions many times. clear wipes that cache. If you insist, you can use clearvars to remove previous runs.
My personal opinion is that left-over variables should have no effect. To achieve that, I pre-allocate variables prior to loops, so there is never any confusion (expect when I make a mistake in my code, I'm only human). With good coding practices you should never need clearvars. Obvioulsy this is one of many instances where you need to learn the rules to know when to break them.
Steven Lord
el 21 de Mzo. de 2024
Well, clc clears the command window, which I like when starting a script in case I was working beforehand just to clean things up.
IMO choosing to run clc yourself before running a script is fine.
But putting it inside a script so it runs automatically will prevent you from running a script twice to compare the results visually. "Okay, that almost did what I wanted. Let me change one line and rerun the code to see which set of results is better." If the display has been automatically cleared by the script, the previous results may have been lost.
Spaceman
el 8 de Abr. de 2024
Categorías
Más información sobre Function Creation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!