How to clear classes except some variables?

16 views (last 30 days)
Dimitrios
Dimitrios on 12 Nov 2014
Commented: Lockywolf on 11 Jan 2018
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.

Answers (2)

Guillaume
Guillaume on 12 Nov 2014
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 on 12 Nov 2014
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 Comment
Lockywolf
Lockywolf on 11 Jan 2018
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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!