How to clear variable data but not remove the variable from workspace?

22 views (last 30 days)
I want to remove the data from variable, meaning i want to empty the variables present in the workspace. I don't want to delete/remove the variables from the workspace.
  3 Comments
Ajay Paudyal
Ajay Paudyal on 13 May 2020
I have tried that but I have more than 1000 variables in workjspace. And the script I am working on creates different variables names with different run. So, it would be difficult use this V = []; So, I am looking for a command which is already there in matlab can do that for me.
Stephen23
Stephen23 on 14 May 2020
Edited: Stephen23 on 14 May 2020
"And the script I am working on creates different variables names with different run."
Your data are badly designed. Bad data design forces you into writing slow, complex, inefficient code.
Fix your code by using one array (e.g. ND numeric, cell array, structure array, table) and simply accessing it using neat and efficient indexing, etc. This is what experienced MATLAB users would do, and is what the MATAB documentation recommends.
For example, if you had used one simple cell array to store your data, then the solution would be just this:
C(:) = {[]};
Neat, simple, efficient code because of better data design. The twelve hours you have already wasted on this are a good illustration of why your current approach is not recommended: even simple tasks become difficult.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 13 May 2020
Edited: Walter Roberson on 13 May 2020
No command is offered for that purpose.
You could write a function that used whos() to find a list of variables, and assignin('caller') to write to the variable.
You might want to take advantage of empty() as in
evalin('caller', sprintf('%s = %s.empty;', VARIABLENAME, VARIABLECLASS) )
Or I guess you could
evalin('caller', sprintf('%s(:) = []', VARIABLENAME))
  2 Comments
Ajay Paudyal
Ajay Paudyal on 13 May 2020
Thanks for your response. There are more than 1000 variables in workspace, so this method wouldn't be feasible.
Walter Roberson
Walter Roberson on 14 May 2020
Tested on a variety of variable types.
Note that this code does not preserve the fields of a struct.
function empty_all_variables
vars = evalin('caller', 'whos');
for K = 1 : length(vars)
varname = vars(K).name;
try
evalin('caller', sprintf('%s = %s.empty;', varname, vars(K).class));
catch
try
evalin('caller', sprintf('%s(:) = [];', varname));
catch
warning('Problems emptying variable: "%s"', varname);
end
end
end
end

Sign in to comment.


Steven Lord
Steven Lord on 13 May 2020
What do you mean by "empty"? Do you want to delete all the elements, thus eliminating information about the size of the variable?
x = magic(4)
x(:) = []
Do you want to overwrite all the elements with some uniform value? If so depending on whether the data type of the variable support missing or not, if it supports scalar expansion:
x = magic(4)
y = int8(x)
x(:) = missing
y(:) = 0 % int8 doesn't support missing
Or related to that, if you want to change all of the size, the values, and potentially the type:
x = magic(4)
x = missing
Do you want to overwrite with noise?
x = magic(4)
x(:) = rand(1, numel(x))
Or do you have something else in mind?
  2 Comments
Ajay Paudyal
Ajay Paudyal on 13 May 2020
I want to delete all the elements, thus eliminating information about the size of the variable. But there are more than 1000 variables in the workspace and the name is also changing with different "RUN", so looking for something can delete all the elements, thus eliminating information about the size of all the variables
Steven Lord
Steven Lord on 13 May 2020
But there are more than 1000 variables in the workspace and the name is also changing with different "RUN",
That's a discouraged programming pattern. See that page and this one for explanations of why it's discouraged and alternatives you can use instead to reduce the number of variables that you need to create and manage.

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!