how is it possible to remove all the cells??

1 view (last 30 days)
is there a way to clear all the cell arrays at the beginning of the code like the way we do it for variables with clear all? if it is possible i do not want to clear each cell one by one like a{:} = {[]}; is there a way to remove all at once ? regards
  3 Comments
Rik
Rik on 1 May 2017
First off, never use clear all, use clear variables instead.
Now to your question. Is there a reason you can't just overwrite your variable with something like C=cell(size(C));?
samicorp
samicorp on 2 May 2017
@Rik Wisselink that is how you preallocate the cell but my question was how can i remove them all. as @hmi amid mentioned correctly clear all or clear variable would do the job which i did not pay attention to.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 1 May 2017
Use the command
varInfo = whos
by examining the varinfo structure, you can determine which variables are cell arrays. For example, when I called it when I had 11 variables in existence:
varInfo =
11×1 struct array with fields:
name
size
bytes
class
global
sparse
complex
nesting
persistent
Loop over all elements in the structure array and if you find a cell array, initialize it.
for k = 1 : length(varInfo)
if strcmpi(varInfo(k).class, 'cell')
% It's a cell array - initialize it
end
end
Frankly, it might be less complicated just to assign the known names of cell arrays to something. I mean, you know the names in your program, don't you?
  2 Comments
samicorp
samicorp on 2 May 2017
i have already done that. the best way is to assign them one by one. although your way of assigning is also interesting. another question is that since i am working with quite large number of cells what is the fastest way of looping over all these cells? any example would be highly appreciated.
Image Analyst
Image Analyst on 2 May 2017
I'm not sure what you're asking. I showed you how to find all the variables in your workspace that are cells or cell arrays (arrays of cells). And I show you how to loop over all of those. Or are you asking how to loop and visit every cell that is one element of a cell array?
Not sure you know what a cell is completely. See the FAQ for a discussion that should give you a good intuitive feel for what they are and how to use them. http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

Sign in to comment.

More Answers (1)

Jan
Jan on 2 May 2017
Do not use clear all, because it does not only clear variables, but removed all loaded functions from the memory also. reloading them from the slow hard disk wastes a lot of time.
Using a general method to clear variables is not clean also. Better create functions for your codes, such that the workspace is kept clean.

Community Treasure Hunt

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

Start Hunting!