how is it possible to remove all the cells??

조회 수: 1 (최근 30일)
samicorp
samicorp 2017년 5월 1일
댓글: Image Analyst 2017년 5월 2일
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
Rik
Rik 2017년 5월 1일
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 2017년 5월 2일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2017년 5월 1일
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
samicorp
samicorp 2017년 5월 2일
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 2017년 5월 2일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2017년 5월 2일
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.
  댓글 수: 1
samicorp
samicorp 2017년 5월 2일
Thanks for the input. will do it from now on.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by