How to clear classes except some variables?

15 次查看(过去 30 天)
I am looping through a code which generates a class with different parameters every time.In every loop i erase the previous classes but i want some values to remain.Unfortunaltely the code i am using erase all the values.To be more clear a simple example follows:
for ii=1:24
clear classes -except Cp ii input;
output = className(input(ii));
Cp(ii) = output.Cp;
end
Any idea what i am doing wrong?Or is there another way to impliment it? Thanks in advance.

回答(2 个)

Guillaume
Guillaume 2014-11-12
No, clear classes also clear all variables, and there's no way around it.
However, clearing classes in a loop sounds very odd. Why are you doing this and what are you trying to achieve? The only reason you'd want to clear classes would be if the code had changed while some objects are still in memory (I'm not even sure you need to do that in 2014b anymore). This is obviously not the case in your example.

Sean de Wolski
Sean de Wolski 2014-11-12
You shouldn't have to clear classes, this is a nuclear hammer approach. You need to clear classes when you the blueprint of the class, i.e. the class definition file has changed. To clear a specific variable just clear it. To clear a specific class, clear just that class.
clear A classA
Still, unless you're changing the text of the class definition, clearing the class should be unnecessary.
  1 个评论
Lockywolf
Lockywolf 2018-1-11
I am modifying class code in process.
My code essentially has several class definitions and for every problem loops through them finding the best one, and substitutes the name for the actually used class.
Is there a way to clear class definitions, desirably for selected classes only?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by