How to clear workspace from a function?

16 views (last 30 days)
I want to create a function that will perform some maintenance operations at the beginning of the script. For example, this function takes a few arguments that will lead to specific initialisations of paths and variables, and above all, clear the workspace from the previous run.
I want to put it in a specific function as the job requires dozens of lines which are quite irrelevant in the work. And I want my "main' script to be as short, readible and high-level as possible.
Below only the first lines of the function.
function init_step(varargin)
clearvars -except varargin
close all
However if I do 2 consecutive runs, it does not clear the workspace. If I want to, I need to explicitely write the "clear" command in the main script, which I try to avoid.
Is there any way to do it?
Thanks in advance.
Louis Tomczyk
Louis Tomczyk on 17 Jan 2023
@Stephen23, I didn't think about it, thanks.
By the way I didn't know about the "code smell", thanks again.

Sign in to comment.

Accepted Answer

Bora Eryilmaz
Bora Eryilmaz on 16 Jan 2023
Edited: Bora Eryilmaz on 16 Jan 2023
Functions do not keep their variables across calls to them unless a variable is specifically defined in the function using the "persistent" keyword. So, you do not need to try to clear variables in a function workspace in this scenario. Each call to the function has its own variable scope (i.e., workspace), which is already clear when the function starts executing, except of course for the variables that you pass to the function as input arguments.
If the function accesses the variables in the base workspace (using evalin, etc.), that is a different question, in which case you can use the clear command.

More Answers (2)

Hiro Yoshino
Hiro Yoshino on 16 Jan 2023
You should understand Base and Function Workspaces. Note that they are different.
One way you can delete variables from the Function Workspace is that you make your variables "global" so you can have access from anywhere.

Louis Tomczyk
Louis Tomczyk on 17 Jan 2023
Thanks to both of you.
However let me be more precise. Let's call WS1 : the workspace of the script, the main.
I would like to clear WS1 by a command I call in a function I created ()
So that each time I run the script, it runs first my function, that will clear all the variables used in the script:
% my script
init_step("my argument")
When I run it for the first time, in WS1 there will be "a" in it.
When I run it again, I expect the "clear" command in to clear the "a", but it does not.
I hope it is more understandable. Thx again
Louis Tomczyk
Louis Tomczyk on 17 Jan 2023
Actually I am not doing anything really special, it was more curiosity. It was almost a yes or no question.
I would like to accept your answer but it seems I cannot accept a comment as an answer.
Thanks a lot.

Sign in to comment.





Community Treasure Hunt

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

Start Hunting!

Translated by